Thread: Reading ints from text file to an array

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Reading ints from text file to an array

    I can't seem to figure out how to read from a text file and to fill an array.
    For example, lets say that my text file looks something like this:1234P
    100 100 100 100 100 100 100 100 100
    100 100
    100
    15
    2381
    P
    100 100 100 100 100 100 100 100 100
    100 100
    100
    15
    3452
    N
    100 100 100 100 100 100 100 100 100
    100 100
    100
    15
    .
    .
    .
    .
    .
    etc.
    The first number is the student ID, followed by a Pass/No Pass on class assignments, 9 homework scores, 2 midterm scores, the final, and extra credit. I have 11 ID's and grades in the file. I'm supposed to scan the ID's and put them into an array. Then scan the grades (the algorithm is determined by adding up the average of each and making sure that the student meets basic requirement, ie homework must be over 80%, midterms over 60, etc)

    How the HECK do I go about doing this? I've been trying many methods of code and NOTHING seems to work for me. Its depressing. I did manage to open the file successfully... haha.
    How would I link which score is to which ID? How do I go about scanning in each line? I CANT EVEN SCAN IN THE ID's into the array for Pete's sake. Yes, I'm getting a little hysterical. Sorry. Anyway, I do have code written up, some is commented out since I've been trying so many approaches. But frankly, it looks like codevomit. But if it would really make a huge difference, let me know and I'll post it up.

    Thank you for your time! I know this is a lot to read, but I'm desperate to get this program to work!

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    1
    Hi,
    If your text file format is same, then, you can read the first line of text file for ID and store it in an array. The next ID will be at 7th line. So, you have to read 7th line i.e., ignore 6 lines in between and store the value at 7th line in the array repeat this for grades also

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Posting some code would make a world of difference; it's hard to help without it.
    So, can you read a whole line in as a string?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    #include <stdio.h>
    #define MAX_SIZE 50
    int openFile(FILE* spGrades);
    int readData(FILE* spGrades, int* stuID[MAX_SIZE], int i, int* ID);
    int readHWScores(FILE* spGrades, int i, int HWaverage);
    
    
    int main (void)
    {
    FILE* spGrades;
    
    int* stuID[MAX_SIZE];
    int HW1;
    int HW2;
    int HW3;
    int HW4;
    int HW5;
    int HW6;
    int HW7;
    int HW8;
    int HW9;
    int exam1;
    int exam2;
    int final;
    int i;
    char grade;
    int* ID;
    int HWaverage;
    
    openFile(spGrades);
    readData(spGrades, stuID, i, ID);
    readHWScores(spGrades, i, HWaverage);
    
    return 0;
    }
    
    int openFile(FILE* spGrades)
    {
    if (!(spGrades = fopen ("BG_Scores.txt", "r")))
           {
            printf("\aError opening student grade file\n");
           return 100;
           }// if open input
    
    else
           printf("Open success\n");
    }
    
    int readData(FILE* spGrades, int* stuID[MAX_SIZE], int i, int* ID)
    {
    if(spGrades){
           while(fscanf(spGrades, "%d", &ID)!=EOF);
    }
    
    /*for (i=1; i <=EOF; i+6)
    fscanf( spGrades,"%d\n", &ID);*/
    stuID[i] = ID;
    
    for (i=0; i<=10; i++)
    printf("%d\n", stuID[MAX_SIZE]);
    return 0;
    }
    
    int readHWScores(FILE* spGrades, int i, int HWaverage)
    {
    int HW1;
    int HW2;
    int HW3;
    int HW4;
    int HW5;
    int HW6;
    int HW7;
    int HW8;
    int HW9;
    
    if(spGrades){
           while(fscanf(spGrades, "%d %d %d %d %d %d %d %d %d", &HW1, &HW2, &HW3, &HW4, &HW5, &HW6, &HW7, &HW8, &HW9)!=EOF);
    }
    printf("%d", HW1);
    
    /*for (i=3; i <=EOF; i+6)
    fscanf( spGrades, "%d %d %d %d %d %d %d %d %d\n", &HW1, &HW2, &HW3, &HW4, &HW5, &HW6, &HW7, &HW8, &HW9);
    HWaverage = ((HW1 + HW2 + HW3 + HW4 + HW5 + HW6 + HW7 + HW8 + HW9)/9);
    printf("%d\n", HWaverage);
    printf("%d\n", HW1);*/
    
    
    
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Like I said, its a mess. Sorry about that. Some things are commented out because I was trying different methods. Sandeep1508, I did try ignoring the 6 lines, I did i+=6, i+6, but nothing works. I end up printing out trash when I try to print the array that the ID's are supposed to be stored in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in text file to an array?
    By Le23ron in forum C Programming
    Replies: 15
    Last Post: 02-12-2012, 11:02 PM
  2. trouble reading a text file into an array
    By Ciaran in forum C Programming
    Replies: 15
    Last Post: 04-04-2011, 05:14 PM
  3. reading a string from a text file into a 2d array
    By duelord in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2009, 07:29 AM
  4. Reading a list of ints from file into an array
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-10-2008, 06:45 AM
  5. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 03:17 PM