Solved

File pointer function

Posted on 2006-11-17
6
219 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

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

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

910 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

22 Experts available now in Live!

Get 1:1 Help Now