File operations

I working on a program that summerizes text documents and I need a way to find the length of the longest and shortest lines in the file and display it in a log file. I've already done a character count and line count. Also say I wanted to created different logfiles for each document i summerize how can this be achieved without overwriting the previos log file?
heres what i've done so far

I working on a program that summerizes text documents and I need a way to find the length of the longest and shortest lines in the file and display it in a log file. I've already done a character count and line count.

int main (void)
{
...
while ((c = fgetc(file)) != EOF)
{

Instead of fgetc() use fgets() to read an entire line. Then you can add a loop to continue doing the part you have already done, and you can check the length of the line for the min and max.

Or start a counter to count each character and when you read the '\n' test that counter against the current min & max. Then reset the counter to 0 and continue.

I recommend reading the first line and set min & max to the length of that line. That would be a good starting point.

does the last line of the file contain '\n' ? If not, then your program will not count it. fgets() will fix that problem because it reads all lines. Getting the length of the longest line is trivel too -- just use strlen() to get the length of the line read by fgets().

The size of the file can not be determined by either your program or using fgets() becuase in MS-Windows os reading text files converts "\r\n" into just "\n". It will be easier to get file length by calling fstat() or stat().

file=fopen("summaryData.txt","w");
fputs("avgchar",file);

you forgot to close the file after writing to it. This will cause your program to eventually run out of file handles. And the fputs() statement is probably incorrect -- all it is doing is printing the same hard-coded text "avgchar" in the file each time. Use something like this:

As far as why not to use EOF is concerned, simple it reads the data from the file one too many times. For detailed explanation, see an excellent tuts by Mr.WaltP and if possible read all of them , they are good.

And btw, where are you opening the file which is passed to the "longest( ) " function. If you are opening it somewhere else then post the entire code.

If you are not opening the "file" stream, then thats what is causing the crash. Before using the file stream you have to open it like:

>> while ((fgets( line, BUFSIZE, file )!=EOF))//why shoudn't I use EOF is there something better?

You really should learn to read the documentation about the functions that you use. If you had, you would have found out that fgets() returns a char pointer or NULL when end-of-file is reached. Don't just toss functions at a program without knowning what they do. you can google for most fiunctions, such as "man fgets" will return the man page for that function.

As far as why not to use EOF is concerned, simple it reads the data from the file one too many times. For detailed explanation, see an excellent tuts by Mr.WaltP and if possible read all of them , they are good.

Actually, it's feof() that's the problem. EOF is a good thing to use, it just has to be used with a function that returns it... which fgets() does not...

on MS-Windows os it will not be the same as the file size because it does not include one of the two line terminating character(s). To get the file size add one to counter for each line read except the last line. The last line may or may not contain '\n', so the program has to check the last character to see if it should also account for the '\r' character.

btw I'm a she...:o I tried changing the !=0 to NULL, I tried incooperating the if (counter<longest) within the while braces but longest is still returning zilch. So I took out fgets(since I thought it was th e problem) and tried

Please, stop coding if you know nothing about what you are doing. At least look up the functions before you indiscriminately toss at your program like darts at a dartboard.

Well I guess I should just toss in my hat and quit then, i'm kinda new at this and I did use the tutorial given as my guide and this what I gathered from it if its wrong then i'm sorry but at least i tried.

I'm getting a Debug assertion failed with fgets.c.Seriously, wouldn't it be easier to use something other fgets()? (Just asking don't crucify me)

The debug assertion error fails because the file pointer is NULL, not because fgets() does not work. fname[] is an unitiailzied string which will cause fopen() to return NULL instead of a valid pointer to the FILE object. Your program should check for a vaid fopen() return value.

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...