簡體   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