From file to 2D arrays

This is a discussion on From file to 2D arrays within the C Programming forums, part of the General Programming Boards category; Hello I have to scan this information from a file into arrays like: Arrays: employeeNumber[5][4] lastName[5][13] firstName[5][13] payGroup[5][11] hoursWorked[5][100] File: ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    From file to 2D arrays

    Hello

    I have to scan this information from a file into arrays like:

    Arrays:

    employeeNumber[5][4]

    lastName[5][13]

    firstName[5][13]

    payGroup[5][11]

    hoursWorked[5][100]


    File:

    0001:Satriani:Joe:6:38.0
    0002:Vai:Steve:1:44.5
    0003:Morse:Steve:10:50.0
    0004:Van Halen:Eddie:3:25.75
    0005:Petrucci:John:8:42.25
    0006:Beck:Jeff:3:62.0

    how would I scan the file into 2D arrays?
    Last edited by arya6000; 11-24-2007 at 04:50 PM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    They're 1D arrays, or do you mean have employeeNumber[line][4] ?

    Either way, read the lines with fgets(), scan each line with sscanf().

    If you want 2D arrays, work out how many records you have and allocate memory accordingly.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    I edited the post so now its more clear, I want to store all those information in array so I can do farther calculations on them in other functions

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Have you tried anything like
    - opening your file
    - reading each record in a big line
    - parse the line into your arrays.

    Parsing usually jus means that you are breaking up a bunch of text data into its parts for analysis. You did that with the English language in grammar school. Use the string library to at least come up with a guess on how to do that part of your homework.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You'll need a loop to go over each line, making sure you say inbounds of your max records.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by zacs7 View Post
    You'll need a loop to go over each line, making sure you say inbounds of your max records.
    I want to do that, but I don't know how I would stop the loop, for regular strings I use "NULL" or "\n" as a stopping point, but for the file it has to go to the next line.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    I figured it out, I can use !EOF

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    fgets() returns NULL when it fails to read (ie at the end of the file).
    Code:
    while(fgets(line, sizeof(line), fp) != NULL && i < MAX_RECORDS)
    {
        /* ... */

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Thanks zacs

    before reading your massage I wrote the program using scanf, but the compiler complains about the line with the "while (fp != EOF);"

    This is what I have so far:


    Code:
    main()
    {
      FILE *fp;
      int i = 0, j = 0, payGroup;
      char employeeNum[10][5], firstName[10][12], lastName[10][12];
      double workedHours[10];
      fp = fopen("employees.dat", "r");
      if (fp) {
      do {
         fscanf(fp, "&#37;4[^:]:%12[^:]:%12[^:]:%d:%lf\n", employeeNum[i], firstName[i],
         lastName[i], &payGroup, workedHours[i]);
         i++;
         }while(fp != EOF);
               }
       else
        printf("Cannot read employees.dat file\n");
    
    }

    Any idea why the compiler is complaining?

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    > fscanf(fp, "%4[^:]:%12[^:]:%12[^:]:%d:%lf\n", employeeNum[i], firstName[i], lastName[i], &payGroup, workedHours[i]

    The fscanf function requires pointers to the variables you want it to change. Inteerestingly, fscanf is also a good choice for reading this particular format.

    Also, fp is a pointer, which can only be compared with other file pointers. fp will never be EOF.

    I'm not really sure you need 2d array for half of these variables. Let's see if we can simplify a little bit:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct employee_t 
    {
        char   employeeNumber[5];
        char   lastName[13];
        char   firstName[13];
        int    payGroup;
        float  hoursWorked;
    };
    int
    process( struct employee_t ar[6], FILE * finp )
    {
        if( finp != NULL ) {
            size_t idx = 0;
            int scan = 0;
            do {
                scan = fscanf( finp, "%4[^:]:%12[^:]:%12[^:]:%d:%f\n",
                    &ar[idx].employeeNumber, &ar[idx].lastName, &ar[idx].firstName, 
                    &ar[idx].payGroup, &ar[idx].hoursWorked );
                if( ferror( finp ) ) {
                    printf( "Error reading input file line %ul\n", ( unsigned long )idx + 1 );
                    return -1;
                }
                idx++;
            }
            while( scan == 5 );
            return 0;
        }
        return 1;
    }
    int 
    main( void )
    {
        struct employee_t my_employees[6];
    
        const char * filepath = "C:/Documents and Settings/JCK/Desktop/samp.txt";
    
        FILE * finp = fopen( filepath, "r" );
        size_t idx = 0;
        if( finp != NULL ) {
            if( process( my_employees, finp ) == 0 ) {
                for( idx = 0; idx < sizeof my_employees / sizeof my_employees[0]; idx++ )
                    printf( "%s: %s, %s\tGroup: %2d Hours: %2.2f\n", 
                        my_employees[idx].employeeNumber, 
                        my_employees[idx].lastName, 
                        my_employees[idx].firstName, 
                        my_employees[idx].payGroup,
                        my_employees[idx].hoursWorked );
    
                fclose( finp );
            }
        }
        else {
            printf( "Couldn't open input file\n" );
        }
        return 0;
    }
    There. Correct, errors all nice and handled, and the file format's been read. Don't always jump to 2d or 3d arrays: often you dont need them.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    have you read this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM

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