?
Solved

File pointer function

Posted on 2006-11-17
6
Medium Priority
?
225 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 46

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 46

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 46

Accepted Solution

by:
Kent Olsen earned 200 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

Technology Partners: 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!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-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.

719 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