Thread: Find the maximum value in a column of a text file

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    3

    Find the maximum value in a column of a text file

    Hi everyone.

    I am a new member. I have some experience with C++ (not a lot) and I want to switch to C. I have problem with finding the maximum value in a column. This is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        FILE *fp;
        fp = fopen ("C:\\rocket1.txt", "r");
        float time, altitude, velocity, acceleration, amax, bmax;
        char *save;
        fgets(save, 95, fp);
        while(!feof(fp))
        {
            sscanf(save, "%f %f %f %f", &time, &altitude, &velocity, &acceleration);
            printf("%f\n",velocity);
            free(save);
            fgets(save, 95, fp);
        }
        rewind(fp);
        fgets(save, 95, fp);
        sscanf(save, "%f %f %f %f", &time, &altitude, &velocity, &acceleration);
        amax = velocity;
        free(save);
        fgets(save, 95, fp);
        while(!feof(fp))
        {
            sscanf(save, "%f %f %f %f", &time, &altitude, &velocity, &acceleration);
            if(amax<velocity)
                amax = velocity;
            free(save1);
            fgets(save, 95, fp);
        }
    
    
        printf("%s %f\n", "the m aximum is", amax);
        fclose(fp);
        printf("%s", "Enter something:");
        getchar();
        return(0);
    }
    The first loop will print out all of numbers in the velocity column. The second while loop is looking for the maximum value in that column. It is supposed to print out the max velocity in the end of the second loop. However, when I build and run the program, it's crashed. I run debugger and received "Segmentation fault" error. When I eliminate the second loop, it run just fine. Can you guy help me to point out the error in this code. Thank you very much. I use Code::Block for text editor.
    Last edited by binbk; 07-28-2014 at 06:12 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    what exactly are you free()-ing?

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    3
    You're right. Thank you very much for pointing it out. Only dynamic pointer needs to be freed after done with using it to prevent memory leaked. I don't need to do that for static pointer (fix me if I am wrong). But even I get rid of the free(), the program is still crashed.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    After commenting out the "free()":

    Code:
    /*
    main.c||In function 'main':|
    main.c|9|warning: unused variable 'bmax'|
    main.c|11|warning: 'save' is used uninitialized in this function|
    ||=== Build finished: 0 errors, 2 warnings ===|
    */
    You need to either allocate memory for the pointer variable "save", else declare it as a character array.

    You should also check that the file opened successfully before trying to use it.

    Code:
    if((fp = fopen ("C:\\rocket1.txt", "r")) == NULL)
    {
        // print error message
        // take any other actions (if the program relies on this file, perhaps exit)
    }
    Also -> FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    In the future, you should provide a sample of the input file so we can try running the program. I pretty much just gave it a quick overview this time around.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not allocate any memory for save to point to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2014
    Posts
    3
    Thank Matticus and Laserlight to point out my problems and give me some advice. I am really appreciate that. I change the pointer save to an array and change the method of determine end of file. The program run smoothly now. However, I wonder if I want to use pointer, what should I do? I assumed that allocate memory for the pointer mean assign an address for it to point to(fix me if I am wrong).
    Hear is the working code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *fp;
        fp = fopen ("C:\\rocket1.txt", "r");
        float time, altitude, velocity, acceleration, amax;
        char save[50];
        fgets(save, 95, fp);
        while(fgets(save, sizeof(save), fp) != NULL)
        {
            sscanf(save, "%f %f %f %f", &time, &altitude, &velocity, &acceleration);
            printf("%f\n",velocity);
            fgets(save, 95, fp);
        }
        rewind(fp);
        fgets(save, 95, fp);
        sscanf(save, "%f %f %f %f", &time, &altitude, &velocity, &acceleration);
        amax = velocity;
        fgets(save, 95, fp);
        while(fgets(save, sizeof(save), fp) != NULL)
        {
            sscanf(save, "%f %f %f %f", &time, &altitude, &velocity, &acceleration);
            if(velocity>amax)
            {
                amax = velocity;
            }
            fgets(save, 95, fp);
        }
    
        printf("%s %f\n", "the maximum is", amax);
        fclose(fp);
        printf("%s", "Enter something:");
        getchar();
        return(0);
    }
    And here is the file
    1.0000000e+01 2.9265380e+03 5.0821200e+02 4.3231640e+01
    2.0000000e+01 1.0170240e+04 9.2798610e+02 4.0723180e+01
    3.0000000e+01 2.1486260e+04 1.1832420e+03 1.0328000e+01
    4.0000000e+01 3.3835080e+04 1.1882285e+03 -9.3307000e+00
    5.0000000e+01 4.5250830e+04 1.0899705e+03 -1.0320900e+01
    6.0000000e+01 5.5634490e+04 9.8935650e+02 -9.8019000e+00
    7.0000000e+01 6.5037960e+04 8.9134700e+02 -9.8000000e+00
    8.0000000e+01 7.3461430e+04 7.9334750e+02 -9.7999000e+00
    9.0000000e+01 8.0904910e+04 6.9534750e+02 -9.8001000e+00
    1.0000000e+02 8.7368380e+04 5.9734700e+02 -9.8000000e+00
    1.1000000e+02 9.2851850e+04 4.9934700e+02 -9.8000000e+00
    1.2000000e+02 9.7355320e+04 4.0134750e+02 -9.7999000e+00
    1.3000000e+02 1.0087880e+05 3.0334400e+02 -9.8008000e+00
    1.4000000e+02 1.0342220e+05 2.0534500e+02 -9.7990000e+00
    1.5000000e+02 1.0498570e+05 1.3402000e+02 -4.4660000e+00
    1.6000000e+02 1.0610260e+05 2.6304000e+02 3.0270000e+01
    1.7000000e+02 1.1024650e+05 6.7618500e+02 5.2359000e+01
    1.8000000e+02 1.1962630e+05 1.2929950e+03 7.1003000e+01
    1.9000000e+02 1.3610640e+05 2.1234700e+03 9.5092000e+01
    2.0000000e+02 1.6209570e+05 3.1700000e+03 1.1421400e+02
    2.1000000e+02 1.9950640e+05 3.8340050e+03 1.8587000e+01
    2.2000000e+02 2.3877580e+05 3.8779450e+03 -9.7990000e+00
    2.3000000e+02 2.7706530e+05 3.7799500e+03 -9.8000000e+00
    2.4000000e+02 3.1437480e+05 3.6819500e+03 -9.8000000e+00
    Last edited by binbk; 07-29-2014 at 08:45 AM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should look up tutorials on memory allocation in C ("malloc()" and "free()" functions).

    There's a bit on it here: Pointers in C - Tutorial - Cprogramming.com
    (cntl+F "dynamic allocation" to find where that part starts)

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    on lines 10,15,18, ...
    you (binbk) have
    fgets(save, 95, fp);
    but save is only 50 chars
    so you could overwrite 45 bytes that follow save's 50 bytes.
    I would suggest doing something like:
    Code:
     
      ....
      char save[50] ;
      fgets(save,sizeof(save),fp);
    .... 
       fgets(save,sizeof(save),fp);
    ...

  9. #9
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Code:
    unsigned int get_numbers(char* str, double* const num, const unsigned int siz)
    {
        char* end;
        unsigned int i;
        for( i = 0; i < siz; ++i ) {
            str = strtok(i? NULL : str, " \t\n");
            if( !str ) { break; }
            num[i] = strtod(str, &end);
            if( end == str ) { break; }
        }
        return i;
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming: Find maximum and minimun.
    By TamaraKM in forum C Programming
    Replies: 1
    Last Post: 03-07-2012, 07:52 PM
  2. Reading a text file and sorting a column of numbers
    By txmusic in forum C Programming
    Replies: 9
    Last Post: 02-26-2012, 09:49 PM
  3. Find Maximum!
    By alireza beygi in forum C Programming
    Replies: 2
    Last Post: 01-06-2012, 05:41 PM
  4. Best way to find maximum number
    By mutombo in forum C Programming
    Replies: 3
    Last Post: 02-27-2009, 04:36 AM
  5. Writing to a column in a text file
    By olland in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 06:40 AM