Solved

Wrong number of lines in a file??? :S

Posted on 2004-10-04
18
201 Views
Last Modified: 2010-04-15
Hi there,

I'm loading a file into memory based on the number of lines in the file. However for some reason it keeps coming up with the number of lines -1:

      char c='\0';
      int ln=0;
      do{
            c=fgetc(file);
            if(c=='\n')
                  ln++;
      }while(c!=EOF);

I could just start ln=1, but I don't think that's the right way to do it???
0
Comment
Question by:Unimatrix_001
  • 7
  • 7
  • 3
  • +1
18 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 12216445

Hi Unimatrix 001,

A couple of things to note here.

1)  fgetc() returns an integer, you're compacting it into a char.  There are some subtleties there that might have an effect on your end-product.

2)  In this program, ln can not become -1 unless it wraps (1, 2, 3, .... MAX_INT, MAX_INT-1,MAX_INT-2, ... -3, -2, -1) which is extremely unlikely or is overwritten by some other process.

When you're printing the line count, I suspect that you're simply printing out 'c' instead of 'ln'.


Good Luck,
Kent
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12216534

One more thought.  If you're still having trouble, try rewriting the loop to look like this:


     in c;
     int ln=0;

     while (1){
          c=fgetc(file);
          if(feof (file))
              break;
          if(c=='\n')
              ln++;
     }

Again, Good Luck,
Kent

0
 
LVL 22

Expert Comment

by:grg99
ID: 12216595
I'd turn your loop around, just in case the file is completely empty, do the EOF() test FIRST at the top of the loop.

Doing that will not only make the program handle an empty file correctly, it should fix the counting problem.

Also you might want to think about whether a file with 80 characters of text but no ending "\n" should be considered to be zero lines or one line.



0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 12216613
Hi Kent,

I've tried changing the loop to what you suggested, but no change unfortunately! :( I output ln when I check how many lines there are.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12216625

Can you post the entire function?  Something else appears to be at work here.

Kent
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 12216633
Welcome to the question grg! :) Here's what I've got:


      while(!feof(file)){
            c=fgetc(file);
            if(c=='\n')
                  ln++;
      }


It's still not coming up with the correct number of lines... Also yes, if there is a line with characters on it but no '\n' then it should be considered a line. Note that the last line in the file fits this example if that makes a difference.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12216684


How about posting the entire function, with the function header?

Kent
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 12216725
int howManyLines(char *);

int howManyLines(char *fileName){
      
      FILE *file;
      file=fopen(fileName, "r");

      char c='\0';
      int ln=0;

      while(!feof(file)){
            c=fgetc(file);
            if(c=='\n')
                  ln++;
      }

       if(fclose(file)==EOF){
            displayError();
              return -1;
       }
      
      return ln;
}


That's it! I've removed a couple of error checks but apart from that, that's it.... Thanks.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12216736

Ahha.

if the close fails, you return -1 as the line count.

You should call perror() here to get a detailed error description.


Kent

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 12216742
noted.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 12217022
Anybody got ideas on what's causing it not to read the entire file though and just miss off the last line?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12217058
Maybe your last line don't have a newline.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 12217070


Sure.

It actually IS reading the entire file.  You make no provision to increment ln if the last line is missing the new-line character.  You've already done most of the work so I've taken the liberty of adding a "last line check" flag to your code:



int howManyLines(char *fileName){
     
     FILE *file;

     int c;
     int ln=0;
     int LineLength = 0;

     file=fopen(fileName, "r");
     while(!feof (file)){
      switch (fgetc (file)){
          case EOF:
            break;
          case '\n':
            ln++;
            LineLength = 0;
            break;
          default:
            LineLength++;
       }
     }
     if (LineLength)
       ln++;

      if(fclose(file)==EOF){
          perror();
          return -1;
      }
     
     return ln;
}



Kent
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 12217099
That'll be the ticket! :D Thanks Kent! I wasn't quite sure on how I could sucessfully check for a missing endline character! Enjoy the points. :-)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12217112
Have you noticed that I posted the answer first?

>Maybe your last line don't have a newline.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12217123
It was obvious and very recursive here at Experts-Exchange.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12217155
Hi Jaime,

Did you notice that before you posted, grg99 actually made reference to it in his earlier post?

>Also you might want to think about whether a file with 80 characters of text but no ending "\n" should be considered to be zero lines or one line.


It was but one of several things that needed help.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 12217158
Jamie - That was answered before you came to this question. See my second comment last sentence. I just didn't know how to check for it. Thanks anyways.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now