#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    District of Columbia
    Posts
    16
    Rep Power
    0

    Need help printing an array from a text file


    Hello again.

    I need a quick bit of help printing an 3x3 array (stored on a text file) which is part of a structure. I have developed the following:

    [code]
    typedef struct{
    int name
    value[3]
    }var
    ......
    {
    fscan(myfile, "%i %i %i", &var[i][0], &var[i][1], &var[i][2]);
    fscan(myfile, "%i %i %i", &var[i][0], &var[i][1], &var[i][2]);
    fscan(myfile, "%i %i %i", &var[i][0], &var[i][1], &var[i][2]);
    }
    .....
    for(i=0;i<3;i++){
    printf("VARIABLES | %i %i %i\n", var[i].name);
    printf("%i", var[0].name);

    /*here's where I get stuck. I am unsure how to input the values. So far I have something like:*/

    printf("%i %i %i\n", var.value[0]);

    /*for the first set of three, but don't think it is correct...*/

    }
    [code]

    Am I on the right track, any help/ ideas?


    Thanks a million.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Show some code that compiles, I think you have more than a few problems in the little bits you showed me.

    There is something wrong in the way you used the "code" tags, please check how to use them.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    District of Columbia
    Posts
    16
    Rep Power
    0
    Thanks for coming to the rescue again Mitakeet.

    I can't quite seem to get anything to compile today.....

    What I am trying to achieve is something like the following, however, importing the numerical data from a text file, which will store the data in a 3x3 array.

    PHP Code:
    [code]#include <stdio.h>[/code] 
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    District of Columbia
    Posts
    16
    Rep Power
    0
    Here's the code, I have read the FAQ on code tags but I must be having a bad day.

    Code:
    #include <stdio.h>
    
    typedef struct{
            int name;
            int value[3];
    }var;
    
    
    int main(void)
    {
     int i;
     var data[3];
     
     data[0].name = "A";
     data[0].value = "011";
     data[1].name = "B";
     data[1].value = "203";
     data[2].name = "C";
     data[2].value = "020";
    
     for(i=0; i<3; i++){
    	 printf("VARIABLE | &d &d &d\n", i, data[i].name);
    	 printf("%d |", data[0].name);
     	 printf("&s\n", data[0].goals);
     	 printf("%d |", data[1].name);
     	 printf("&s\n", data[1].goals);
     	 printf("%d |", team[2].name);
     	 printf("&s\n", team[2].goals);
     }
     return 0;
    }
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Int values do NOT get quotes arround them and you can't (well, you can, technically, but I don't want you confused) assign character values.

    String values must be added using either strcat, strcpy or placed one character at a time. If you do it one character at a time, you use single quotes.

    For instance:

    Ded Wrong:
    Code:
        data[0].name = "A";
        data[0].value = "011";
    Right:
    Code:
        data[0].name = 65;//integer value for the character 'A'
        data[0].value = 11;//preceeding zeros are interpreted as octal
        //011 in octal is 9 decimal
    
        //say you want to get the string 'ABC'
        char data[4];
    
        strcpy(data, "ABC");
        //why the extra character?  For the terminating NULL!
    
        //or
        data[0] = 'A';
        data[1] = 'B';
        data[2] = 'C';
    
    //NULL as a character
        data[3] = '\0';
    //or NULL as integer
        data[3] = 0;
    Does any of this help you get on the right track? You are REALLY lost right now, I suggest some reading.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    District of Columbia
    Posts
    16
    Rep Power
    0
    O.K.

    I do follow, really I do! I am making a lot of silly mistakes, I have been reading, I found myself going around in circles. I can see the finished product in my mind, however, right now I feel like a moth to a flame.

    Anyway, enough mindless waffle, I guess I could reword my code to something like:

    Code:
    typedef struct{
            int name;
            char value[3];
    }var;
    
    ....
    strcpy(data[0].value, "012\0");
    strcpy(data[1].value, "234\0");
    strcpy(data[2].value, "123\0");
    ....
    Thanks for you patience, I am getting the hang of this stuff!
  12. #7
  13. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You have an off-by-one memory overwrite. Your 'value' needs to be 4 bytes wide, not three. You are getting closer!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    District of Columbia
    Posts
    16
    Rep Power
    0
    I see, the struct should read:

    Code:
    typedef struct{
            int name;
            char value[4];
    }var;
    I hope to tie everything in soon. I need to pass the reading of the array and dumping of the array as functions. I have limited knowledge of this process, but have found enough literature on the web to keep me going.

    Wish me luck!
  16. #9
  17. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You got it feller!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo