×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Counting lines in a file

Counting lines in a file

Counting lines in a file

(OP)
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!

RE: Counting lines in a file

> 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.

RE: Counting lines in a file

(OP)
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.

RE: Counting lines in a file

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

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


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

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
if ( buff[0] == '\n' )
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
595,13,"2008-10-26 16:15:15","2008-10-18 23:45:00"RECORD_COUNT:8\n
 

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

RE: Counting lines in a file

(OP)
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.

RE: Counting lines in a file

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close