Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read file from third line? 1

Status
Not open for further replies.

ngen

Technical User
Mar 18, 2001
5
NL
Dear all,

Sorry to bother you all. I wish to read in a file from second (or more) lines. In other words, I don't want to read in the file from the beginning. I am using the basic functions from <stdio.h>, fread, fseek, fread etc.

Please help. Thousand thanks.

Best regards
Jordan
 
If it's a normal text file, about all you can do is skip over the first two lines, like with:

void readFile()
{
const int lines_to_skip = 2;
char buff[1024];
FILE *fp = fopen(&quot;c:\\test.txt&quot;,&quot;r&quot;);
if (fp)
{
//Skip &quot;lines_to_skip&quot; lines
for (int i = 0; i<lines_to_skip && !(feof(fp) || ferror(fp)); i++)
fgets(buff,1024,fp);

// read rest of file
while(!(feof(fp) || ferror(fp)))
{
fgets(buff,1024,fp);
printf(buff);
}
}
fclose(fp);
}
:) I just can't help it, I like MS...:)
 
Dear Dave,

First of all, thanks for your tips and help. It does help :)... but... is there another way not reading the file line by line after skipping the first few lines? For example, is it possible to set the position to some location of the file and start reading from that???

I am sorry to bother you again. Please help. And, thousand thanks again for your help. Wish you all the best.

Best regards
Jordan
 
You can skip over characters with fseek(fp,16,0); for example, but unless it's a binary file and you know the length of the lines this wont help too much. I'm not sure now if your problem is the skipping, or reading in the file.

You can read the whole (rest of the) file in in one go if you want (here it gets easier to not mix ANSI and Windows functions). This skips some bytes at the front and reads the rest into a dynamically allocated buffer:

void readFile1()
{
const int bytes_to_skip = 11;
char * pBuff;
int fp = _open(&quot;c:\\test.txt&quot;,_O_RDONLY|_O_BINARY);
if (fp>0)
{
// Skip beginning
_lseek(fp,bytes_to_skip,0);

// read rest of file
long len = _filelength(fp)-bytes_to_skip;
pBuff = new char[len+1];
_read(fp,pBuff,len);
*(pBuff+len) = 0;
printf(pBuff);
delete [] pBuff;
}
_close(fp);
}
:) Click here if this helped! :)
vvvvvvvvv
 
Dear Dave,

Thanks for your help indeed.

Both of your tips are very helpful indeed... But, since I am really poor at C++... This is what I wish to do:

--------THE FILE STRUCTURE------------
<something here which is to be skipped>
<blank>
<blank>
<blank>
<THE REAL DATA STARTS HERE>
--------END FILE STRUCTURE------------

I wish to read from the <THE REAL DATA...>. That's why I have to skipped the first four lines. In fact there is some data at the first line (which is the header of this file), but I wish to skip that too.

Please help. Of course at the same time, I will try your tips again :) just to improve my C skills as well as to find some inspiration. Thousand thanks.

Wish you all the best.


Best regards,
Jordan


 
I'm not sure what more I can say, I've shown how to skip lines in a text file, and how to skip over the start of a binary file. Your description of file format is still rather vague, but as far as I can see it can only really be one of the two possibilities I've already dealt with.

There's also the question of how you read the data when you've done with skipping. Functions like fscanf() skip whitespace anyway. I personally tend to use the iostream and related classes if I'm doing simple file I/O. :) Click here if this helped! :)
vvvvvvvvv
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top