Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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???
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


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

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

Avatar of grg99
grg99

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.



Avatar of Unimatrix_001

ASKER

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.

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

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


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

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

Ahha.

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

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


Kent

noted.
Anybody got ideas on what's causing it not to read the entire file though and just miss off the last line?
Maybe your last line don't have a newline.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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. :-)
Have you noticed that I posted the answer first?

>Maybe your last line don't have a newline.
It was obvious and very recursive here at Experts-Exchange.
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.
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.