繁体   English   中英

如何在c编程中读取文本文件到数组结构的数据?

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

通过使用一个功能,程序应该从每一行读取数据,作为有关汽车名称,货物类型,重量(全部),长度,拉动该车型所需马力以及该类型汽车数量的信息。 在从文件中读取数据然后在控制台中显示时,数据必须存储在结构数组中。 必须使用strsub()函数。

遗憾的是,文本文件中的数据不会打印到控制台。 我认为问题可能在于我使用指针,但我现在已经读了五遍以上的章节了。 如果它打印,只有乱码打印。

#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
  1. 数组大小错误。
    char type[1];
    printf(" Type: %s ", train->type);

当你将它传递给printf或访问printf时, Type不能保持\\0字符,你有未定义的行为。

解:

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

  1. 范围问题。
    for (int i = 0; i < count; ++i){
        data* train = &train[i];
        outFile(train);
    }

这里train作为阵列具有在更高的优先级train作为指针,从而你不知不觉传递train作为数组outFile功能。

解:

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

建议:使用sscanf功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM