Thread: how to read from a text file into an array of records?

  1. #1
    Registered User
    Join Date
    Feb 2002

    Question how to read from a text file into an array of records?

    i have a question.. is it possible to read the data from a text file into an array of records? and how can this be done?

    for example if the text file contains names, pins and accounts of customers, how can i put them into an array of record ?

    Thanx a lot

  2. #2
    Registered User
    Join Date
    Feb 2002
    One way to do this is to have a fixedsize struct containing the data:

    struct Person{
        char *szName[64];
        long *pin;
        // + other data, fixed size
    When you read the data from your (binary) file, read sizeof(Person) number of characters from the file, and typecast or memcopy the data to a Person:

    Person pers;
    memcpy(&pers, bufferfromfile, strlen(bufferfromfile));
    Person *pers = (Person*)bufferfromfile; // note! do not delete the allocated memory for bufferfromfile.
    To create an array, store a header in the file that tells you how many records there is in the file. And it can tell you how big each record is, and where it is located in the file, but this would be optional. Now read in the header, and create a Person array the size you found out from the file. Remember that adding a record to the file must increase the count in the header. Another way is to use dynamic arrays aka std::vector, then you wont need the header for recordcounting. A thing I would use the header for though, is a kind of CRC check to see if the file is valid or corrupt, or changed manually since last time.

    Remember that the file must be stored and read binary. When you write to the file, you typecast the Person struct to char* (void*??) and stream it to the file. This what you can strore an entire struct, and retreaving it by doing the process in reverse.

  3. #3
    Registered User
    Join Date
    Mar 2002

    Cool Think about it

    You can try making a pointer which points to the array and carry out the information from the file you loaded using the code below for example.

    FILE *file; //Creats file pointer
    file = fopen("file.txt", "r"); //opens file in text mode
    if(file==NULL) {
    printf("Error: can't open file.\n");
    //fclose(file); DON'T PASS A NULL POINTER TO fclose !!
    return 1;

    Then after the file is loaded you can make it point to the array or structure you created example.

    int records[50][50]; //makes a 2 dimensional array
    FILE *file; //creates the file pointer
    /////////////or the structure example

    struct Person{
    char *szName[64];
    long *pin;
    // + other data, fixed size
    } record;

    then pass the file you loaded to this struct called Person using the Operators "&" and so on.
    - Read to Learn, and Learn to Read -

  4. #4
    Registered User
    Join Date
    Oct 2002
    #include <conio.h>
    #include <stdio.h>
    int n,a[8] //depending on size of array

    FILE *fin;
    fin=fopen("a:data.c","r") //that reads the file

    and so forth.....

    hopefully this helped

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    If you made a class for the "Person", you can overload the >> stream extraction operator, it will be easier for you, so you can say File >> Person;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read file into array
    By neobarn in forum C Programming
    Replies: 6
    Last Post: 01-09-2009, 12:41 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM