Solved

File pointer function

Posted on 2006-11-17
6
220 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
  • 3
  • 3
6 Comments
 
LVL 45

Expert Comment

by:Kdo
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:Kdo
ID: 17967885


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


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

 

Author Comment

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

Accepted Solution

by:
Kdo 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

770 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