Thread: Reading and Saving data from file

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    48

    Reading and Saving data from file

    So, I have a file with this:

    Champion of Light
    4.5 5.6 7.8 9.3 2.3 0.1 0 0 0 0
    Champion of Darkness
    5.4 6.7 9.9 0.3 2.6 2.4 0 0 0 0
    Champion of Fire
    3.4 5.6 7.7 8.8 2.3 5.8 0 0 0 0

    I'm trying to save the names in a char array and the numbers in a float array and then print it. Been trying for a while and can not do anything. Can anybody give some advice ? I would save them in something like this:

    Code:
    struct PlayerData{
        char name[50];
        float reflex;
        float power;
        float stamina;
        float speed;
        float intelligence;
        float height;
        int injury;
        int exhaustion;
        int yellow_card;
        int red_card;
    };
    
    struct playerdata data[10];
    Last edited by MrPecanha; 06-08-2016 at 01:50 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Read each line with "fgets()". For lines that contain data, use "sscanf()" on the string to extract that data.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    Actually i want to save the name on one array and all the other data in just one float array.
    But with that method i would need to use 1 fget() for every line in the archive ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    Yes, pairs of fgets() calls into two separate buffers.
    From the first buffer you extract the name, and from the second buffer you extract all the attributes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    Quote Originally Posted by Salem View Post
    Yes, pairs of fgets() calls into two separate buffers.
    From the first buffer you extract the name, and from the second buffer you extract all the attributes.
    Tried this out but did not work. Where am I wrong ?

    Code:
    int main()
        {
                
        FILE *fp;
        fp = fopen("Pica das Galaxia FC.txt", "r");
    
        //read file into array
        float numberArray[SIZE];
        char names[3][255];
        float atributes[3][50];
        int i,j;
    
        if (fp == NULL)
        {
            printf("Error Reading File\n");
            exit (0);
        }
        
        
        fgets(names[1], 255, (FILE*)fp);
        fgets(names[2], 255, (FILE*)fp);
        
    
        printf("Name is: %s\n", names[1]);
        
        sscanf(names[2], "%f %f %f %f %f %f %f %f %f %f", atributes[0][0], atributes[0][1], atributes[0][2], atributes[0][3], atributes[0][4], atributes[0][5], atributes[0][6], atributes[0][7], atributes[0][8], atributes[0][9]);                                  
        
        for( i = 0; i < 10; i++)
        {
        printf("Number is: %.1f\n", atributes[1][i]);
        }
        
        fclose(fp);
    
        return 0;
        }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    You didn't compile with enough warnings enabled - to alert you to the fact that your sscanf is passing the wrong parameters.
    Code:
    $ gcc -std=c99 -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 3 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 5 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 6 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 7 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 8 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 9 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 10 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 11 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 12 has type ‘double’ [-Wformat]
    foo.c:16:9: warning: unused variable ‘j’ [-Wunused-variable]
    foo.c:13:9: warning: unused variable ‘numberArray’ [-Wunused-variable]
    Should be
    &atributes[0][0], &atributes[0][1] etc

    > printf("Number is: %.1f\n", atributes[1][i]);
    Don't you think you should print the values you scanned, at index 0 (not 1)

    > fgets(names[1], 255, (FILE*)fp);
    fp is (and can only ever be) a FILE*, so the cast is entirely redundant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    Quote Originally Posted by Salem View Post
    You didn't compile with enough warnings enabled - to alert you to the fact that your sscanf is passing the wrong parameters.
    Code:
    $ gcc -std=c99 -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 3 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 5 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 6 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 7 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 8 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 9 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 10 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 11 has type ‘double’ [-Wformat]
    foo.c:31:3: warning: format ‘%f’ expects argument of type ‘float *’, but argument 12 has type ‘double’ [-Wformat]
    foo.c:16:9: warning: unused variable ‘j’ [-Wunused-variable]
    foo.c:13:9: warning: unused variable ‘numberArray’ [-Wunused-variable]
    Should be
    &atributes[0][0], &atributes[0][1] etc

    > printf("Number is: %.1f\n", atributes[1][i]);
    Don't you think you should print the values you scanned, at index 0 (not 1)

    > fgets(names[1], 255, (FILE*)fp);
    fp is (and can only ever be) a FILE*, so the cast is entirely redundant.
    Yes. I managed to see it out and fix those stuff.
    I am not sure if I can ask this because basically my thread was answered. But for this function, I obtain these warnings: "Passing argument 1 of 'fgets' from incompatible pointer type". Same thing for sscanf. The fgets of "a_stats" What is the problem ?

    Code:
    void read_players1(struct Player1Data data1[]){
        float a_stats [20];    //Temporarily stores data
        FILE *fp1;
        fp1 = fopen("Pica das Galaxia FC.txt", "r");
        if (fp1 == NULL)
        {
            printf("Error Reading File\n");
            exit (0);
        }
    
        fgets(data1[0].name, 255, fp1);
        fgets(a_stats, 255,fp1);
        
        sscanf(a_stats, "%f %f %f %f %f %f %f %f %f %f", &data1[0].reflex, &data1[0].power, &data1[0].stamina, &data1[0].speed, &data1[0].intelligence, &data1[0].height, &data1[0].injury, &data1[0].exhaustion, &data1[0].yellow_card, &data1[0].red_card);
        
        fclose(fp1);
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    But for this function, I obtain these warnings: "Passing argument 1 of 'fgets' from incompatible pointer type". Same thing for sscanf. The fgets of "a_stats" What is the problem ?
    Basically any description on fgets() workings will tell you that argument 1 is a char array and that it will read strings of a certain size. So I would note that a_stats being completely the wrong type is not your only problem. You also have a magic value 255 that you keep using for the buffer size. I have no idea how you got such a number, but it's wrong.

    Ordinarily the first argument to fgets is something like
    Code:
    char buffer [1024];
    Because it's nice and long and you can store a string inside of it.
    Code:
    fgets(buffer, sizeof buffer, fp1);
    sizeof buffer == 1024 * sizeof(char) == 1024 * 1. It's always correct.

    And then after you get buffer you would pass it along to sscanf() like in the example you posted.

  9. #9
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    Ok, done.

    Programs stop's working when I try out this order function. How to solve ? Putting necessary parts:

    Code:
    struct Player1Data{
        char name[50];
        float reflex;
        float power;
        float stamina;
        float speed;
        float intelligence;
        float height;
        int injury;
        int exhaustion;
        int yellow_card;
        int red_card;
    };
    
    /////////////////////////////////////////////////////////////////////
    
    void order(struct Player1Data data1[]){
        int i,j;
        for(i = 0;i < SIZE; i++)
        {
        printf("You want player %d to be in which position (changes player positions) : ", i+1);
        scanf("%d", j);
        data1[10] = data1[i];
        data1[j] = data1[i];
        data1[i] = data1[10];
        
        }
    }
    
    /////////////////////////////////////////////////////////////////////
    
    struct Player1Data data1[(SIZE+1)];

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    I've already shown you how to compile, so you get warnings when you mess up printf/scanf formats.

    You're at the bottom of the same hole again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    Quote Originally Posted by Salem View Post
    I've already shown you how to compile, so you get warnings when you mess up printf/scanf formats.

    You're at the bottom of the same hole again.
    This is about my order function ?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    Yes, your scanf calls are broken again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    Quote Originally Posted by Salem View Post
    Yes, your scanf calls are broken again.
    LOL, forgot something so simple again. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems saving read file data into a structure
    By jcmoney in forum C Programming
    Replies: 3
    Last Post: 11-19-2012, 05:37 AM
  2. Replies: 20
    Last Post: 06-07-2007, 06:38 PM
  3. saving data into a file
    By afrm in forum C Programming
    Replies: 4
    Last Post: 05-22-2006, 09:54 AM
  4. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  5. Reading lines from a file and saving to a variable
    By Rare177 in forum C Programming
    Replies: 1
    Last Post: 06-09-2004, 03:47 PM

Tags for this Thread