Solved

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

Posted on 2004-10-04
18
202 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

863 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

24 Experts available now in Live!

Get 1:1 Help Now