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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting lines in a file

Status
Not open for further replies.

KABDRM

Programmer
Jan 13, 2005
50
0
0
I have one file that ends on a line that has data

COUNT_OF_ATTEMPTS 4
595,13,"2008-10-20 04:40:15","2008-10-13 08:00:00"
294,11,"2008-10-21 04:47:15","2008-10-15 08:51:00"
637,14,"2008-10-22 04:45:15","2008-10-16 08:50:00"
504,7,"2008-10-23 08:30:15","2008-10-16 14:15:00"
123,45,"2008-10-24 16:15:15","2008-10-16 23:45:00"
637,14,"2008-10-25 14:00:15","2008-10-17 20:30:00"
595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"<-EOF here

and one that ends on the next line

COUNT_OF_ATTEMPTS 4
595,13,"2008-10-20 04:40:15","2008-10-13 08:00:00"
294,11,"2008-10-21 04:47:15","2008-10-15 08:51:00"
637,14,"2008-10-22 04:45:15","2008-10-16 08:50:00"
504,7,"2008-10-23 08:30:15","2008-10-16 14:15:00"
123,45,"2008-10-24 16:15:15","2008-10-16 23:45:00"
637,14,"2008-10-25 14:00:15","2008-10-17 20:30:00"
595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"
<- EOF here

How can I tell the difference between the two? I need to add a record_couint record to the end. In the first case, I count 8 records - that's good. This is how my file ends up:

COUNT_OF_ATTEMPTS 4
595,13,"2008-10-20 04:40:15","2008-10-13 08:00:00"
294,11,"2008-10-21 04:47:15","2008-10-15 08:51:00"
637,14,"2008-10-22 04:45:15","2008-10-16 08:50:00"
504,7,"2008-10-23 08:30:15","2008-10-16 14:15:00"
123,45,"2008-10-24 16:15:15","2008-10-16 23:45:00"
637,14,"2008-10-25 14:00:15","2008-10-17 20:30:00"
595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"
RECORD_COUNT:8

But in the second example, it still correctly counts 8 record but adds a blank line

COUNT_OF_ATTEMPTS 4
595,13,"2008-10-20 04:40:15","2008-10-13 08:00:00"
294,11,"2008-10-21 04:47:15","2008-10-15 08:51:00"
637,14,"2008-10-22 04:45:15","2008-10-16 08:50:00"
504,7,"2008-10-23 08:30:15","2008-10-16 14:15:00"
123,45,"2008-10-24 16:15:15","2008-10-16 23:45:00"
637,14,"2008-10-25 14:00:15","2008-10-17 20:30:00"
595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"

RECORD_COUINT:8

while (fgets(InString, 250, InFile) != NULL) {
if (strcmp(InString, "RECORD_COUNT", 12))
RecordCount++;
else
return(RC_FOUND_ADD_MODE);
}
fprintf(InputFile2, "\nRECORD_COUNT:%",RecordCount);

I'd either like it to count it as nine records or put the record count record after the last record and count 8 records.

Thank you!
 
> 595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"<-EOF here
Assuming you don't have lines longer than your buffer, then a result from fgets() which does not have a \n at the end will identify this case.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
My program works fine if there is no \n at the end of the last line of data. It is when there is a \n at the end that it doesn't work correctly. And I can't seem to tell - after I have dropped out of the loop - the difference between the first example of data and the second. I'd like to know that the last line was blank, count it and print RECORD_COUNT:9. But in either case, when I check the InString variable after dropping out of the loop, it's value is \n. So I don't know when to add one to the record count or not.
 
Like I said, does your last line contain \n - true or false.

> 595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"<-EOF here
Unless this is an inaccurate statement, and what you have is either

[tt]595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"\n
<-EOF here[/tt]

or
[tt]595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"\n
\n
<-EOF here[/tt]
That is, a file which ends with an empty line, as opposed to a file which ends with a complete line and a newline.

So the test becomes
[tt]if ( buff[0] == '\n' )[/tt]
instead.

Which seems to make more sense really, since appending the record count to a file which doesn't end with a \n would result in
[tt]595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"RECORD_COUNT:8\n[/tt]


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
When I look at the first file in notepad and go to the end of the file (Ctrl/End), the cursor is just after the last double quote.

595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"|

When I do the same thing in the second file, it is just below the 5 that is in the first postion of the last line of data.

595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"
|

In both cases, the program counts 8 records but in the second example, it puts a blank line between the last line of data and the record count line. I suppose I could tell the user that the file must end with no carrage return after the last line of data, but I'd rather the program could handle it either way.

Actually, when I test what my buffer is after I exit the while loop, it equals 5 - the first character in the last line??? Not \n - using both of the files described above.

Thanks for trying to help. I am not sure if I am explaining this very well.
 
How about:
Code:
while (fgets(InString, 250, InFile) != NULL) {
  if (strcmp(InString, "RECORD_COUNT", 12) && strlen(InString) > 0)
     RecordCount++;
  else
     return(RC_FOUND_ADD_MODE);
}

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top