1st, IMAO you do not have to post the entire program, just the piece you are trying to fix, as you did.
2nd, as Ancient Dragon said, what's the name of the file? There is nothing in fname[]
3rd, please learn to format your code readably. You need consitant indentation. It's better to use SPACEs, not TABs. And it's good to use extra lines between sections, but not between everything:

int longest(void)
{
    char line[BUFSIZE] = {'\0'} ;
    int counter=0;
    int longest=0;
    char fname[128];
    FILE *file;
	
    file=fopen(fname,"r");

    while (fgets( line, BUFSIZE, file )!=0)
    {
        counter=strlen(line);
        if(counter>longest)
        {
            longest=counter;
        }
    }
    return longest; 
}

Another thing I noticed is a cute trick that you should not use:

nlines += c == '\n';/*Line counter*/

Instead use something easier to read:

if (c == '\n')  /*Line counter*/
{
    nlines++;
}

thanks, I didn't see the point in posting the entire code either but I was trying to be cooperative, originally the file was to be opened in main so the filename would be variable. Longest() was to take in the file and return the longest line.
In essence i just need to know why this doesn't work as it should I'd rather not open the file in each function since I have several similar ones to write and that can get tedious.

int longest(FILE *file)

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

	 if(counter>longest)

		{
		 longest=counter;
		}
   
	}
	return longest; 
	
}

1st, IMAO you do not have to post the entire program, just the piece you are trying to fix, as you did.

thanks, I didn't see the point in posting the entire code either but I was trying to be cooperative,

Hmm....

Anyways gettign back to the topic, when you say it doesnt work, why dont you tell us exactly what kind of output are you expecting, what output are you currently getting, what kind of sample data are you supplying to your program. The function on the surface looks fine... just a random thought, try replacing the != 0 with > 0 in the while loop.

that can get tedious

Sure it can. But so can keeping track of where the file pointer is and what state is in. Just passing the file pointer to the file doesn't gaurantee the file pointer is in a useable state/position. For example, if you read the entire file to find the longest string in one function and then try to pass the file pointer to another function to find the shortest string without pointing it back to the start of the file, it will fail. Either way it's tedious. Tedious is a good part of programming. Using loops and containers, etc., can minimize tedium, but paying attention to detail is always tedious.

Hmm....

Anyways gettign back to the topic, when you say it doesnt work, why dont you tell us exactly what kind of output are you expecting, what output are you currently getting, what kind of sample data are you supplying to your program. The function on the surface looks fine... just a random thought, try replacing the != 0 with > 0 in the while loop.

i am expecting a character count of the longest line using a variable text file I created a txt file to test on about nine lines long and the logest line is about 23 characters but currently i'm getting 0 for both longest and shortest functions

update: it works now all i had to do was insert a rewind() before the function call since i calculated the number of lines earlier i was at the end of my file
thanks all!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.