Commands in an if statement in C occurring whether or not the statement is true

For a program I'm writing I need to read a file line by line and print out the longest line in the file, along with the length of that line as well as the total number of lines in the file.

I have this:

char line[100];
char* longLine;
int count, longestLine, temp;

count=0;
longestLine=0;

while(fgets(line,100,inFile) != NULL) {
    temp=strlen(line);  
    if(temp > longestLine) {            
        longLine=line;
        longestLine=temp;
    }
    count++;
}
printf("Longest line: %sLength of longest line: %d characters\nTotal number of lines: %d\n",longLine, longestLine, count);

longestLine and count print correctly, but no matter what, longLine always prints out the last line of the file instead of the longest line. Using print statements I've determined that the if statement in the while loop is only called when a new longest line is found, yet longLine gets changed regardless. Can anyone tell me what I'm doing wrong? Thanks!

Solutions

The problem is that longLine is a pointer. You're going into the if and then setting longLine to point to line... which at the start of the next while has changed. What I suspect you intended to do was copy the contents of line into longLine using strcpy, otherwise, each time through the loop. longLine will always point to the currently evaluating line.

A footnote, your code is incorrect even if the code function correctly. You need to initialize longestLine to zero before it is used. Not all compilers may initialize it to zero - it might have garbage in it. Temp may never be larger than the garbage in longestLine.

Tags: C

Similar questions

What is the difference between now = datetime.datetime.now() and now = datetime.now() in Python?
What is the difference between now = datetime.datetime.now() and now = datetime.now() in Python? Why does datetime need to be repeated?
Why method now in Python is obtained as datetime.datetime.now instead of datetime.time.now?
I would like to know why the method now was implemented under the datetime.datetime label instead of the datetime.time? For example to obtain today's date on python you do the following. but you can't the same for the time now, I mean Instead you have to do the following:
How to compare today's date and date stored in a string (without time)?
With python, How can I check if a date stored in a string has already passed? My current code: The code above gives me the following error: Note that I would not want to work with any time - just the date (this case the treated in 32287708)
Why is there a date.today() but no time.now() in the datetime module?
I am editing some Python and ran across this inconsistency in the datetime module. For the current date I just need: but for time: Any rational for not having a "now()" method (or whatever name) in the datetime.time object? This would be much cleaner: PS: The time module is not an option at this point
Why datetime.now() and datetime.today() show time in UTC and not local time on my PC?
datetime.now() and datetime.today() return time in UTC on my computer even though the documentation says they should return local time. Here's the script I ran: and here's the output: The output of running date right after it is: Why is my installation returning time in UTC? What can I do get those functions to return local time? PS We are in MST, ...
Pandas dataframe filter by date range : today - today+ 1 year
I am able to filter a dataframe using a date range: but I would like to be able to filter for a year

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.