Solved

File pointer function

Posted on 2006-11-17
6
218 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
Comment Utility

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
Comment Utility
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
Comment Utility


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


Kent
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Accepted Solution

by:
Kdo earned 50 total points
Comment Utility


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
Comment Utility
yep I think I'll use the latter thanks a bundle!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

763 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

13 Experts available now in Live!

Get 1:1 Help Now