intriguedandconfused
intriguedandconfused

Reputation: 51

How to read and data from text file to array structure in c programming?

By using a function, the program should read the data from each line as information about a car’s name, cargo type, weight (full), length, required horsepower to pull that car type, and number of cars of that type. The data must be stored in an array of structures as it is being read from the file then displayed in console. The strsub() function must be used.

Unfortunately the data from the text file isn't printing to the console. I think the problem may lie in my use of pointers, but I've read the chapter on it more than five times now. And if it does print, only gibberish prints.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
FILE *fpIn;

void strsub(char buf[], char sub[], int start, int end);
void readFile();

typedef struct {
    char name[10];
    char type[1];
    float weight;
    int length;
    int power;
    int amount;
}data;

// Substring extractor function from book
void strsub(char buf[], char sub[], int start, int end){
    int i, j;

    for (j = 0, i = start; i <= end ; i++, j++){
        sub[j] = buf[i];
    }
    sub[j] = '\0';
}

// Prints out file
void outFile(data* train) {

    printf(" Name: %s ", train->name);
    printf(" Type: %s ", train->type);
    printf(" Weight: %.2f ", train->weight);
    printf(" Length: %d ", train->length);
    printf(" Horsepower: %d ", train->power);
    printf(" Number in Train: %d ", train->amount);

}

// Reads file
void readFile(){
    int count = 0;
    data train[MAX];

    // Opens file
    if(!(fpIn = fopen("traindata.txt", "r"))){
        printf("Error. File can not be opened. \n");
    }

    // Reads each line of text in file
    while (!feof(fpIn)){
        char buf[MAX+2];
        char weightbuf[5];
        char lengthbuf[3];
        char powerbuf[2];
        char amountbuf[3];

        fgets(buf, MAX, fpIn);
        strsub(buf, train[count].name, 0, 8);
        strsub(buf, train[count].type, 10, 10);
        strsub(buf, weightbuf, 12, 16);
        strsub(buf, lengthbuf, 18, 20);
        strsub(buf, powerbuf, 22, 23);
        strsub(buf, amountbuf, 25, 27);
        train[count].weight = atof(weightbuf);
        train[count].length = atoi(lengthbuf);
        train[count].amount = atoi(amountbuf);
        train[count].power = atoi(powerbuf);
        ++count;
    }

    for (int i = 0; i < count; ++i){
        data* train = &train[i];
        outFile(train);
    }
}

int main(int argc, char *argv[]) {
    printf("This table shows the current Train Cars\n\n");
    readFile();

    return 0;
}

Expected results:
Boxcar    D 44000 55 16 45
Hopper    B 23000 62 18 33
Tanker    G 15000 45 30 12
Autocar   A 30000 37 23 6
Livestock L 56500 50 18 19
Coalcar   C 49300 53 22 100
Flatcar   F 18000 66 15 25

Current Display:
This table shows the current Train Cars

 Name: ����  Type:   Weight: 0.00  Length: 20  Horsepower: 1696  Number in Train: -2112880507

Error?:
data* train = &train[i];
  - Local variable "train" might not have been initialized

Upvotes: 1

Views: 253

Answers (1)

kiran Biradar
kiran Biradar

Reputation: 12732

  1. Wrong array size.
    char type[1];
    printf(" Type: %s ", train->type);

Type cannot hold \0 character as you pass it to the printf or access out of bound, you have undefined behavior.

Solution:

char type;
train[count].type = buf[10]; //Do not call substr function.
printf(" Type: %c ", train->type);

  1. Scope problem.
    for (int i = 0; i < count; ++i){
        data* train = &train[i];
        outFile(train);
    }

Here train as array has higher preference over train as pointer, thus you are unknowingly passing train as array to outFile function.

Solution:

for (int i = 0; i < count; ++i){
    data* pTrain = &train[i];
    outFile(pTrain );
}

Suggestion: use sscanf function.

Upvotes: 3

Related Questions