• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

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???
0
Unimatrix_001
Asked:
Unimatrix_001
  • 7
  • 7
  • 3
  • +1
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
grg99Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Unimatrix_001Author Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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

Kent
0
 
Unimatrix_001Author Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:


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

Kent
0
 
Unimatrix_001Author Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
Unimatrix_001Author Commented:
noted.
0
 
Unimatrix_001Author Commented:
Anybody got ideas on what's causing it not to read the entire file though and just miss off the last line?
0
 
Jaime OlivaresCommented:
Maybe your last line don't have a newline.
0
 
Kent OlsenData Warehouse Architect / DBACommented:


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
 
Unimatrix_001Author Commented:
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
 
Jaime OlivaresCommented:
Have you noticed that I posted the answer first?

>Maybe your last line don't have a newline.
0
 
Jaime OlivaresCommented:
It was obvious and very recursive here at Experts-Exchange.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
Unimatrix_001Author Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now