GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Jul-2004, 15:10
galamagalama galamagalama is offline
New Member
 
Join Date: Jul 2004
Posts: 3
galamagalama is on a distinguished road

how read a specified line from a file


hello!
I'm doing some file i/o programmes for my webpage, but i'm stuck at one prob...
i'm working in C

i know i can read the whole file(text) with _fgets_ command,
but i'd like to read only, i.e. 2nd line, in the file.

is that possible?
tnx!
  #2  
Old 19-Jul-2004, 15:44
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello galamagalama and welcome to GIDForums.

Unless I am mistaken, fgets should only read up to the newline in a file.

Code:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

So, fgets should only read one line at a time.

So, quickly off the top of my head (and untested... )
CPP / C++ / C Code:
fp = fopen("filename.ext","r");

while( fgets(buffer,MAXSIZE,fp) && lineno)
  lineno--;

fclose(fp);

If I am thinking right, buffer should contain the text at "lineno" after running that.

HTH,
d
  #3  
Old 19-Jul-2004, 16:02
galamagalama galamagalama is offline
New Member
 
Join Date: Jul 2004
Posts: 3
galamagalama is on a distinguished road
weeeeeeeeee, it's working
but only for line one in the file...

how could it work for line 2, 3,...etc?
  #4  
Old 19-Jul-2004, 16:05
galamagalama galamagalama is offline
New Member
 
Join Date: Jul 2004
Posts: 3
galamagalama is on a distinguished road
Quote:
Originally Posted by galamagalama
weeeeeeeeee, it's working
but only for line one in the file...

how could it work for line 2, 3,...etc?

nevermind that, i figure it out!
many tnx!
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56
read specified range of lines from file etron C Programming Language 8 18-Feb-2004 09:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.