Thread: Read text file and store it in an array

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Exclamation Read text file and store it in an array

    I have a text file with this in it.
    Smith,John,91,95,100
    Jones,Mary,91,90,88
    Simon,Simple,52,61,70
    Clause,Kris,75,80,82

    I have a Project8.h file with:
    typedef struct{
    char firstName[20];
    char lastName[20];
    int hw1;
    int hw2;
    int hw3;
    }student;

    and a Project8.c file with:
    int main() {
    int length = 20;
    student grades[length];
    readData( "scores.txt", grades, length );
    printAveForHW( 2, grades, length );
    printHighest( 3, grades, length );
    return 0;

    These I know are correct because they are given and I have to use the names.

    The problem I'm trying to solve is this:

    The readData function reads data from a file named filename and stores it into an array of students of length len. You may create an intermediate file as I did in my example to replace commas with spaces so that reading strings works as desired.

    This is what I have but I'm completely lost. I changed the commas to spaces but I don't know what to do after this. I don't know how to read the data from a file named filename and store it into an array of students of length len.

    #include "Project8.h"

    void readData( char *filename, student arr[], int len );

    int main(){
    FILE *from, *to;

    from = fopen("scores.txt", "r");

    to = fopen("temp.txt", "w");

    char c;
    while(fscanf(from, "%c", &c) != EOF){
    if(c != ',')
    fprintf(to, "%c", c);
    else
    fprintf(to, " ");
    }

    fclose(from);
    fclose(to);

    void readData( char *filename, student arr[], int len )

    FILE *filename = fopen("temp.txt", "r");
    char lastName[20];
    char firstName[20];
    int hw1;
    int hw2;
    int hw3;
    while(fscanf(filename, "%s %s %d %d %d", &lastName, &firstName, hw1, hw2, hw3)

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    use code tage and indent your code. Your code is very difficult to read.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You don't need intermediate file.
    Just read line by line, and use strtok with delimiter ",".
    Code:
    while( NOT EOF ) {
        read line; 
        tokens = parse(line,",");          // use strtok in real code.
        // convert token to number,etc...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read and store text file as an array
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:57 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Replies: 5
    Last Post: 08-01-2007, 06:17 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 10:51 AM