Thread: Help filling array from text file

  1. #1
    Registered User
    Join Date
    Nov 2012

    Help filling array from text file

    Hi, I can't seem to figure out where to go from here. I am trying to fill a one-dimensional array from the text file "AG_Midterms_Averaged.txt". How do i scan the text file and then store it into an array?

    The text file is 40 lines of test scores like...


    Here is my code so far
    #include <stdio.h>
    #include <stdlib.h>
    // Function Declarations
    int getData (FILE* spDataIn, long int* studentId, int* score1, int* score2);
    void midtermsAveraged (int score1, int score2, float* scoreAvg);
    int writeMidtermsAvg (FILE* spDataOut, float scoreAvg);
    int main (void)
    // Local Declarations
    FILE* spDataIn;
    FILE* spDataOut;
    long int studentId;
    int score1;
    int score2;
    float scoreAvg;
    // Statements
       if (!(spDataIn = fopen("/Users/sincerevillanuevaiii/Documents/CIS15/HW8/AG_Midterm.txt", "r")))
        printf("\aError opening employee file\n");
        return 100;
       if (!(spDataOut = fopen("AG_Midterms_Averaged.txt", "w")))
        printf("\aError opening employee fileee\n");
        return 102;
       } // of open output
       while (getData
             (spDataIn, &studentId, &score1, &score2))
         midtermsAveraged (score1, score2, &scoreAvg);
         writeMidtermsAvg (spDataOut, scoreAvg);
        } // while
        return 0;
    } // main
    /* ==============================getData=============================
       Reads student data from text file.
          Pre   spDataIn is an open file
                studentId, score1 and score2 are passed by address.
          Post  Reads student info
    int getData (FILE* spDataIn, long int* studentId, int* score1, int* score2)
    // Local Declarations
       int ioResult;
    // Statements
       ioResult = fscanf(spDataIn, "%ld %d %d", studentId, score1, score2);
       if(ioResult == EOF)
       return 0;
       return 1;
    } // getData
    /* ==============================midtermsAveraged=============================
       Determines average test scores based on two tests
          Pre   score1 and score 2 contrain the variables
          Post  scoreAvg copied to address
    void midtermsAveraged (int score1, int score2, float* scoreAvg)
    // Statements
    *scoreAvg = ((score1+score2)/2);
    } // midtermsAveraged
      Writes only the averaged score to the new text file.
         Pre   spDataOut is an open file
               scoreAvg has values to write
         Post  Data written to file
    int writeMidtermsAvg (FILE* spDataOut, float scoreAvg)
       // Statements
       fprintf(spDataOut, "%.2f\n", scoreAvg);
       return 0;
    } // writeMidtermsAvg

  2. #2
    Registered User
    Join Date
    May 2012
    I think you are reading the file correctly and storing the values where they should be stored.
    Why can't you fill the array? I mean, if this code is working fine you should be able to store the values in an array, you just need to iterate through the array index and do what you are already doing with the variables.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Hey so is this a skeleton program your teacher gave you?

    I think you may want to declare an array and then read the data points into it like this because right now it looks like you are just using your file to store the data as an array; however, i do not have a lot of experience with pointers so i'm not 100% sure what you are doing here.
    #Define N 40
    int a[N];
    int k=0;
    while ((fscanf(filename,"%i",&a[k]))==1)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help filling list array from file
    By jstout in forum C Programming
    Replies: 4
    Last Post: 04-29-2011, 02:04 PM
  2. filling array from file
    By Thegame16 in forum C Programming
    Replies: 20
    Last Post: 12-10-2010, 09:16 PM
  3. Filling Array Of Structures From A File
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 11-26-2008, 09:59 AM
  4. filling an array with a simple file
    By arya6000 in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 01:34 PM
  5. filling an array from a file using fscanf...
    By stodd04 in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 08:55 AM