Click here to Skip to main content
15,913,388 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I am working on an application that should read a text file using C file handling functions.
The requirement is to read the file line by line using fgets(), but if the length of any line is more than 1000 characters it should not read the current line any more and should move into the next line.

Can any one plz help me how to move the file pointer to the next line while it is in the current line.

Thanks
Dev
Posted

 
Share this answer
 
If reading from file using fgets() and the line is less than the specified size, the terminating new line character is stored in the buffer. If the line is longer, the terminating new line character is not stored. You may use the strchr() function to determine the presence of the new line character.

If it is not present, call fgets() again until a new line character has been read into the buffer and continue with processing this next line.
 
Share this answer
 
Comments
Devadutta Achary 15-Feb-12 5:24am    
Thanks Jochen for the reply.
I tried the same thing that you have suggested.But I wanted to know if there is any File handling functions to jump to the begining next line if the current line has more than the specified characters.
Thanks,
Dev
Jochen Arndt 15-Feb-12 5:35am    
No, there is no such function. But you may write your own:
int c = feof(f) ? EOF : 0;
while (c != EOF && c != '\n') c = fgetc(f);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900