Solved

File pointer function

Posted on 2006-11-17
6
223 Views
Last Modified: 2010-08-05
I have a function that is supposed to return the length of the longest line in a file stream ca anyone give me an idea why this function keeps returning zero?
//definition
int longest(FILE *file)

{
      char line[BUFSIZE] = {'\0'} ;
      int counter=0;
      int longest=0;
      
    while (fgets( line, BUFSIZE, file )!=0)//BUFIZE defined as 512
      {
      
       counter=strlen(line)-1;
      

       if(counter>longest)

            {
             longest=counter;
            }
   
      }
      return longest;
      
}

// Function call(fp was opened in main)
max=longest(fp);
0
Comment
Question by:wkellya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17967385

Hi wkellya,

Several thoughts come to mind.

-- Have you already read from the file?  The file may not be positioned at BOI.
-- Have you issued an fseek() on the file?
-- Are you sure that the file is non-empty?

Try putting an fseek(file, 0, SEEK_SET) or rewind(file) before you start the loop.

Also, for debug purposes, execute a perror("exiting longest") right before the return statement.  There may be a file error that you're not detecting.

Kent
0
 

Author Comment

by:wkellya
ID: 17967854
Success! I inserted a rewind just before the function call in main and now i'm getting the count for longest line.Unfortunately i'm not getting a reading for my shortest line I keep getting zero even when I remove all empty lines from my source document meanwhile my shortest line is about four characters.
int shortest(FILE *file)

{
      char line[BUFSIZE] = {'\0'} ;
      int counter=0;
      int shortest=0;
      
      
    while ((fgets( line, BUFSIZE, file )!=0))
      {
      
       counter=strlen(line)-1;
      

       if(counter<shortest)

            {
             shortest=counter;
            }
      }
      
      return shortest;
      
}
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17967885


  shortest starts at zero.  You're unlikely to read a line smaller than that.   :)


Kent
0
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!

 

Author Comment

by:wkellya
ID: 17968052
Opps! sorry dumb question :)  but then how should I initialize shortest ?
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 50 total points
ID: 17968065


Set it arbitrarily high, like 1,000,000.  Then test for shortest == 1,000,000 before exit.

Or change the test if you want the shortest non-blank line.

  if(shortest == 0 || counter<shortest)
0
 

Author Comment

by:wkellya
ID: 17969974
yep I think I'll use the latter thanks a bundle!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

636 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