Thread: C - File IO and 2D Arrays

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    1

    C - File IO and 2D Arrays

    I am a student working on an assignment for school, and I am currently stuck with a problem. I am having trouble filling in my 2D array with integers from a text file. When I go to run and compile this it gives me absurd random numbers in my print out. I think I am not calling the function correctly or my pointer may be not in the correct position.

    Text File:
    Code:
    43
    Samantha Octavia Ivan Henry
    100 90 65 70
    99 50 70 88
    88 90 98 100
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 100
    
    
    //function prototype
    void getNames(int students, char names [][SIZE], FILE * inputFile);
    void getGrades (int assignments, int students, int grades[][students], char names[][SIZE], FILE * inputFile);
    //void calcGrades(int assignments, int students, int grades [][students], char names[][SIZE]);
    //void printGrades(int assignments, int students, int avg, int grades [][students], char lettergrade [] [students], char names[][SIZE]);
    
    
    
    
    int main()
    {
    //variables
    int students;
    int assignments;
    char string0 [100];
    char char0;
    FILE * inputFile;
    
    
    //Open File
    inputFile = fopen ("input.txt", "r");
    
    
    //input
        if(    inputFile == NULL)
        {
            printf ("Unable to open file input.txt");
            exit(EXIT_FAILURE);
        }
        else
        {
            printf ("How many students?\n ");
            fscanf (inputFile, "%d", &students);
            printf ("%d", students);
            printf ("\nHow many assignments?\n ");
            fscanf (inputFile, "%d", &assignments);
            printf ("%d\n", assignments);
        }
    //variables
    char names[students][SIZE];
    int grades[assignments][students];
    
    
    
    
    //passing variables to funcs
        getNames (students,names,inputFile);
        getGrades (assignments,students,grades,names,inputFile);
    //   calcGrades(assignments, students, grades, names);
    //  printGrades(assignments, students, avg, grades, lettergrade, names);
    
    
        //fclose(inputFile);
    
    
        return 0;
    
    
    
    
    }
    
    
    void getNames (int students,char names [][SIZE], FILE * inputFile)
    {
    //variables
    int i;
    
    
        for (i=0; i<students; i++) //counter for array size
        {
           printf ("\nEnter name for Student %d: ",i);
           fscanf (inputFile, "%s", names[i]);
           printf ("%s",names[i]);
        }
    }
    
    
    void getGrades (int assignments, int students, int grades [][students], char names[][SIZE], FILE * inputFile)
    {
    //variables
    int i;
    int j;
    
    
    //counter loop
       printf ("\n");
    
    
       for (i=0; i < assignments; i++) //counter for array size
        {
           for (j=0; j < students; j++)
           {
                printf ("\nEnter grade for Assignment %d for %s: ",i,names[j]);
                fscanf (inputFile, "%d", &grades[j][i]);
           }
           printf ("%d", grades[i]);
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think your biggest problem is that you try to print grades like this:
    Code:
    printf ("%d", grades[i]);
    If you want to print grades, you should print each element out -- there is no real shortcut like printing grades[i] or something like that. What happens when you try to anyway is usually what you saw; grades[i] is a variable length array, and when you print an array, as opposed to an array element, what you see is a memory address (where the array is stored) instead of something useful.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also this loop seems incorrect:

    Code:
     
     
       for (i=0; i < assignments; i++) //counter for array size
        {
           for (j=0; j < students; j++)
           {
                printf ("\nEnter grade for Assignment %d for %s: ",i,names[j]);
                fscanf (inputFile, "%d", &grades[j][i]);
           }
           printf ("%d", grades[i]);
        }
    The outer loop should probably be the students, and the inner loop should probably be the assignments.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O and Arrays
    By John Gaden in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2008, 02:46 PM
  2. File I/O with arrays
    By 20,000leeks in forum C++ Programming
    Replies: 32
    Last Post: 09-18-2006, 02:16 AM
  3. Arrays and File I/O
    By Darklighter in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2006, 08:40 PM
  4. File I/O with 2D arrays
    By hypertension in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 08:47 AM
  5. Arrays from a file
    By frenchfry164 in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2002, 11:54 AM

Tags for this Thread