Help with putting data into an array

This is a discussion on Help with putting data into an array within the C Programming forums, part of the General Programming Boards category; Hi guys and gals, I'm new hear and to programming. I'm currently taking a class but I need a little ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    6

    Help with putting data into an array

    Hi guys and gals,

    I'm new hear and to programming. I'm currently taking a class but I need a little help getting data from a txt file and putting it into array for me to be able to work with it. Here is sample of the code the teacher provides. The bottom section is the data that would be on the txt file.

    #include <stdio.h>
    #include "perry.h"

    #define MAXNAME 41 /* Extra slot for \0 char that ends all strings!! */
    #define MAXSCORES 9 /* Extra slot for -1 sentinel. */
    #define MAXFILE 81

    int main(void)
    {
    char infile[MAXFILE], outfile[MAXFILE];
    FILE *fin, *fout;
    void produceAverageAndVarianceFile(FILE *fin, FILE *fout);

    printf("Enter input filename: ");
    scanf("%s", infile);
    printf("Enter output filename: ");
    scanf("%s", outfile);

    fin = Fopen(infile, "r");
    fout = Fopen(outfile, "w");

    produceAverageAndVarianceFile(fin, fout);
    }


    /*Smith,Joe 7 77 86 89 90 76 90 84
    Jones,Ed 5 65 56 77 59 66
    Thomas,Chris 5 58 90 77 92 88
    Doinck,Wilson 3 44 48 58
    Dover,Ben 7 43 87 84 99 39 49 94
    Wonka,Willie 8 47 85 80 56 76 92 85 67
    Stein,Franken 6 75 72 78 81 82 99
    Franklin,Ben 7 100 100 99 76 98 89 93
    Wilson,James 1 88*\
    [/CODE]

    I'm a little confused with there being char and int in the data. If you can help out that would be great. Thank you

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    I need a little help getting data from a txt file
    [...]
    I'm a little confused with there being char and int in the data
    You're new here but you surely read the FAQ and you know the drill, right?:
    - what did you try
    - what was the result
    - what do you expect

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    This is what i tried to do to get the data into the array

    Code:
    #include <stdio.h>
    #include "danny.h"
    #define MAXNAME 41 /* Extra slot for \0 char that ends all strings!! */
    #define MAXSCORES 9 /* Extra slot for -1 sentinel. */
    #define MAXFILE 81
    int main(void)
    {
    char infile[MAXFILE], outfile[MAXFILE];
    float scores [9][MAXSCORES];
    int numofscore = 0;
    int i;
    int j;
     
    char name[8][MAXNAME];
    FILE *fin, *fout;
    // void produceAverageAndVarianceFile(FILE *fin, FILE *fout); 
    printf("Enter input filename: ");
    scanf("%s", infile);
    printf("Enter output filename: ");
    scanf("%s", outfile);
    fin = Fopen(infile, "r");
    {
    fscanf (fin, "%s %f", name, &numofscore)!= EOF;
    }
    fout = Fopen(outfile, "w");
    {
    printf("%s %f \n", name, &numofscore);
    }
    /*produceAverageAndVarianceFile(fin, fout); 
     
     
    */
    }
    It pulled only the name but got some larger number for the numofscore.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    1- check the status of your call: what happens if fopen() fails?
    2- %f is expected to be associated with a double, and you provided [fill in the blanks]

  5. #5
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I think you would first need to create an array of structures to store the data.

    Code:
    struct data {
        char firstname[10]; 
        char lastname[15];
        int number1;
        int number2;
        int number3;
        int number4;
        int number5;
        int number6;
        int number7;
        int number8;
    } Information[9];   /* 9 for nine students */
    Last edited by happyclown; 01-20-2009 at 02:54 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    what is "perry.h"? I mean please show us the code. maybe that'll help?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    Here is perry.h
    Code:
    void *Malloc(size_t size)
    {
        void *ptr;
    
        if ((ptr = (void *) malloc(size)) == NULL)
        {
            perror("malloc");
            exit(1);
        }
        return ptr;
    }
    
    
    void *Realloc(void *ptr, size_t numMembers)
    {
         void *newptr;
    
         if ((newptr = (void *) realloc(ptr, numMembers)) == NULL)
         {
             perror("Realloc");
             exit(1);
         }
         return newptr;
    }
    
     
    void *Calloc(size_t numMembers, size_t size)
    {
        void *ptr;
    
        if ((ptr = (void *) calloc(numMembers, size)) == NULL)
        {
            perror("malloc");
            exit(1);
        }
        return ptr;
    }
    
    
    FILE *Fopen(char *file, char *mode)
    {
        FILE *fp;
    
        if ((fp = fopen(file, mode)) == NULL)
        {
             printf("Fopen error on %s!\n", file);
             exit(1);
        }
        return fp;
    }
    
    
    void Fclose(FILE *fp)
    {
        if (fclose(fp) != 0)
        {
            printf("Fclose failed!\n");
            exit(1);
        }
    }
    It's a bit of a understanding issue. I'm confused with getting the data from the txt file and moving it into an array so i can do calucations of averages. I just need help with getting the data into the array.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    the last lab i did to put in data we used this
    Code:
    for (i = 0; i < ARY_SIZE; i++)
    {
    fscanf (fptr,
    "%d", &array[i]);
    }

  9. #9
    Registered User
    Join Date
    Nov 2008
    Location
    Bay Area, CA
    Posts
    53
    Had I been able to take the 15BG class as well this quarter (being broke and jobless sucks), given the way Perry presented the sample data to use, my starting point would be in chapter 10 on page 639. Assuming you're using the same book. If you study Perry's sample data and figure 10-22 closely enough, you'll see what I mean. The trick, I suppose, would be to adapt the basic concept of the example program that follows to use fscanf to read the stuff from the file, rather than scanf from the keyboard.

    Were you in Ahrens class last quarter?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void Fclose(FILE *fp)
    {
        if (fclose(fp) != 0)
        {
            printf("Fclose failed!\n");
            exit(1);
        }
    }
    1. I've NEVER seen fclose() fail [aside from when passed a NULL pointer - in which case it crashed badly].
    2. For good measure, if it happens that fclose DOES fail, exiting might not be the BEST idea ever - there may be other files that you wish to close, etc. Just printing an error message if it happens is probably not a bad idea, but exiting may be a bit crude.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    If you created a structure, with an array in the structure to store each line, you can then use fgets() to read each line into that array. You can then process each line in that array using sscanf() and break it up and store the item in the other elements of the structure.

    Code:
    #define LINELENGTH 40
    
    struct data {
        char firstname[10]; 
        char lastname[15];
        char line[LINELENGTH];           / * stores each line in here */
        int number1;
        int number2;
        int number3;
        int number4;
        int number5;
        int number6;
        int number7;
        int number8;
    } Information[MAXSCORES];
    
    count = 0;
    
    while( ( fgets( Information[count].line, LINELENGTH, fin) != NULL ) && ( count <  MAXSCORES ) )
    {
        sscanf( Information[count].line, "%s,%s %d %d %d %d %d %d %d %d", Information[count].lastname,
        Information[count].firstname, &Information[count].number1, &Information[count].number2,
        &Information[count].number3, &Information[count].number4, &Information[count].number5,
        &Information[count].number6, &Information[count].number7, &Information[count].number8 );
    
        count++;
    }
    Last edited by happyclown; 01-20-2009 at 04:46 PM. Reason: typo
    OS: Linux Mint 13(Maya) LTS 64 bit.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by happyclown View Post
    I think you would first need to create an array of structures to store the data.

    Code:
    struct data {
        char firstname[10]; 
        char lastname[15];
        int number1;
        int number2;
        int number3;
        int number4;
        int number5;
        int number6;
        int number7;
        int number8;
    } Information[9];   /* 9 for nine students */
    In MANY cases using an array instead of different distinct member variables for the "number" values will make life easier (for example if you want to average them).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    Quote Originally Posted by Sorin View Post
    Had I been able to take the 15BG class as well this quarter (being broke and jobless sucks), given the way Perry presented the sample data to use, my starting point would be in chapter 10 on page 639. Assuming you're using the same book. If you study Perry's sample data and figure 10-22 closely enough, you'll see what I mean. The trick, I suppose, would be to adapt the basic concept of the example program that follows to use fscanf to read the stuff from the file, rather than scanf from the keyboard.

    Were you in Ahrens class last quarter?
    HA haa. . . I was in his class. I'm thinking of dropping the class cus of money reason too.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    Quote Originally Posted by matsp View Post
    In MANY cases using an array instead of different distinct member variables for the "number" values will make life easier (for example if you want to average them).

    --
    Mats
    Does C know to put the char into the right place and the int into the right place?? What if the is only 5 numbers?? Will the other 4 have 0 place in the values??

  15. #15
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by Tek12x View Post
    Does C know to put the char into the right place and the int into the right place?? What if the is only 5 numbers?? Will the other 4 have 0 place in the values??
    If you declare the structure outside(global) any function, including main(), I think the array elements will all be initialized to zero.

    EDIT: I've have just tested this by declaring a global structure(with an int array). I printed out the array's values, and they were all zero. So they have all been initialized to zero.

    fscanf() will only read what it can for each line, and put the values into array number[i]. For the values fscanf() can't read(because there is nothing to else read), the array elements will be already initialized to zero.

    And the structure should look like this.
    Code:
    struct data {
        char firstname[10]; 
        char lastname[15];
        char line[LINELENGTH];           / * stores each line in here */
        int number[7];
    } Information[MAXSCORES];
    Last edited by happyclown; 01-20-2009 at 07:20 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 19
    Last Post: 12-17-2007, 01:57 AM
  3. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21