Thread: How to read file and store into an array

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    12

    How to read file and store into an array

    hey i have a file that i need to read and then store into an array.

    the file is called ass2.txt and looks like this:
    Code:
    7453842
    Gregory     Harrison
    4
    CSCI104 0 56
    IACT111 0 44
    INFO112 0 75
    CSCI321 0 89
    
    
    7545349
    Quentin Resnick
    3
    CSCI104 0 55
    IACT101 1 76
    INFO101 0 78
    there are two records in the file as seen above i need to read the records one by one and then store them into an array called gRecs[].

    this is only part of my code that i have so far to read the file:

    Code:
    bool ReadFile()
    {
    FILE *openfile;
    
    
    openfile=fopen(ass2.txt,"r");
    /*Checks if file exists, removing auto generated file if not*/
    if (openfile == NULL){
    fprintf(stderr,"Error Opening %s",ass2.txt);
    remove(ass2.txt);
    return false;
    }
    
    /*store records in array*/
    while(fscanf(open file, "%s", gRecs){
    fclose(openfile);
    
    
    return true;


    im not sure if this is the correct way any help would be appreciated cheers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,833
    The reading and parsing of a record is going to be more complex than that. I suggest writing a function to read a record; this function can then be called in a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    12
    Quote Originally Posted by laserlight View Post
    The reading and parsing of a record is going to be more complex than that. I suggest writing a function to read a record; this function can then be called in a loop.

    do you have any ideas on what would be used in the function to read the records? im abit lost.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,833
    You would need to read the record part by part. I presume you have defined a struct type for the records?

    Test with a file containing a single record first. Worry about multiple records later.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    12
    Quote Originally Posted by laserlight View Post
    You would need to read the record part by part. I presume you have defined a struct type for the records?

    Test with a file containing a single record first. Worry about multiple records later.
    yes this is what the overall code looks like thus far:

    Code:
    
    
    #include <stdio.h>
    #include "ass2.h" /*Contains function prototypes*/
    
    
    #define TEXT_FILE_NAME "ass2.txt"
    #define BIN_FILE_NAME "ass2.dat"
    #define MAX_RECS 100
    
    
    typedef enum {eEnrolled,eProvisional,eWithdrawn} StatusType;
    
    
    typedef struct{
    double subjectcode;
    char enrollment;
    int mark;
    } SubjectType;
    
    
    typedef struct {
    long StudentNum;
    char firstname;
    char lastname;
    double subjects[8];
    } StudentRecordType;
    
    
    StudentRecordType gRecs[MAX_RECS];
    int gNumRecs=0;
    
    
    void PrintRecord(int i);
    int FindRecord(long StudentNum);
    
    
    bool ReadFile()
    {
    FILE *openfile;
    
    
    openfile=fopen(ass2.txt,"r");
    /*Checks if file exists, removing auto generated file if not*/
    if (openfile == NULL){
    fprintf(stderr,"Error Opening %s",ass2.txt);
    remove(ass2.txt);
    return false;
    }
    
    
    while(fscanf(open file, "%s", gRecs){
    }
    fclose(openfile);
    
    
    return true;

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,833
    You might want to fix your structs first: subject code most likely should be a string, and likewise for the names. You might also want a member to record the number of subjects for the student.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read a file in c and store it in an structure?
    By Sravan Kumar in forum C Programming
    Replies: 5
    Last Post: 02-08-2017, 02:10 PM
  2. Read Strings in from text file and store into array
    By RRTT in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2011, 04:52 AM
  3. How to read a line and store it into an array?
    By david.jones in forum C Programming
    Replies: 5
    Last Post: 05-09-2011, 05:07 PM
  4. Read text file and store it in an array
    By look2hook in forum C Programming
    Replies: 2
    Last Post: 12-03-2010, 11:47 PM
  5. read and store text file as an array
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:57 PM

Tags for this Thread