无法将文件读入C语言中的结构数组

时间:2014-03-11 04:13:56

标签: c arrays struct

我有一个输入文本文件,其中包含:

1234,123456781,"Smith","Bob A.",ABC, 123,="A01"
1234,123456782,"Jones","Tom Jr",ABC, 123,="A03"  
1234,123456783,"Michaels","Mike",ABC, 123,="A03" 

这是输出应该是什么样子。

Last Name, First Name            Term      ID      Course   Section 
-------------------------------- ----  ---------   ------   ------- 
Jones, Tom Jr.                   1234  123456782   ABC123     A03 
Michaels, Mike                   1234  123456783   ABC123     A03 
Smile, Bob A.                    1234  123456781   ABC123     A01  

我在将文本文件中的信息读入结构数组时遇到了很多麻烦。它必须以这种方式实现,然后打印/显示给用户。

到目前为止,这是我的代码......已经有好几天了!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//The values included in the source text file.
typedef struct items{
    char fname[16];
    int cat;
    char lname[16];
    char sub[4];
    int term;
    int  sID;
    char sec[4];
}STRING;

void formatOut(){
    //Output format
    printf("\n\n\nLast Name, First Name            Term      ID      Course   Section");
    printf("\n-------------------------------- ----  ---------   ------   ------- ");
}



int main(){

int count;
count=0;
int i=0;

FILE* fp;
struct items item[200];

//Finds The File
char file_name[25];
printf("Enter the name of file that includes your data. Be sure to write your file extension.\n(For example, source.txt is a valid entry, source is invalid)\n");

gets(file_name);

fp = fopen(file_name,"r");

//Actions to take incase the file does not exist.
if(fp == NULL){
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
}
//Making things neater.
printf("__________________________________\n\n");
printf("The contents of %s file are :\n", file_name);


//Retrieve data from txt file

do{ fscanf(fp,"%d %d %s %s %s %d %s", &item[i].term, &item[i].sID, item[i].lname, item[i].fname,item[i].sub, &item[i].cat, &item[i].sec);
    printf("Term:%d\n", item[count].term);
    printf("Student ID:%d\n", item[count].sID);
    printf("LastName:%c\n,", item[count].lname);
    printf("FirstName:%s\n", item[count].fname);
    printf("Subject:%s\n", item[count].sub);
    printf("Category:%d\n", item[count].cat);
    printf("Section:%s\n", item[count].sec);
    i++;
}while(fscanf(fp,"%d %d %s %s %s %d %s", &item[i].term, &item[i].sID, item[i].lname, item[i].fname, item[i].sub, &item[i].cat, item[i].sec>1));

/*
for(i=0; i<n; i++){
    fscanf(fp,"%d", &item[count].term);
    fscanf(fp,"%d", &item[count].sID);
    fscanf(fp,"%s", &item[count].lname);
    fscanf(fp,"%s", &item[count].fname);
    fscanf(fp,"%s", &item[count].sub);
    fscanf(fp,"%d", &item[count].cat);
    fscanf(fp,"%s", &item[count].sec);

    printf("Term:%d\n", item[count].term);
    printf("Student ID:%d\n", item[count].sID);
    printf("LastName:%s\n,", item[count].lname);
    printf("FirstName:%s\n", item[count].fname);
    printf("Subject:%s\n", item[count].sub);
    printf("Category:%d\n", item[count].cat);
    printf("Secton:%s\n", item[count].sec);
    count++;
}
 */

fclose(fp);
formatOut();

}

这就是我目前的输出结果:

Term:1301
Student ID:0
LastName:\364
FirstName:
Subject:
Category:0
Section:



Last Name, First Name            Term      ID      Course   Section
-------------------------------- ----  ---------   ------   ------- 

显然,结构数组没有收到正确的值。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

您应该将lname更改为数组,与fname相同,这些结构必须相应地填充转换为特定数据类型的char数组。

typedef struct items{
    char fname[16];
    int cat;
    char lname[16];
    char sub[4];
    int term;
    int  sID;
    char sec[4];
}STRING;

您需要使用sID, term, cat转换atoi(),因为您的代码中有一个整数,您将从文件中读取一个char *。

答案 1 :(得分:1)

我认为最佳做法是设置一些断点并逐步DEBUG查看是否存在任何问题。这也是提高编码和调试技能的最佳方法。

请:

  1. 将while语句更改为:

    while(fscanf(fp,"%d,%d,\"%[^\"]\",\"%[^\"]\",%[^,],%d,=\"%[^\"]\"", &item[i].term, &item[i].sID, item[i].lname, item[i].fname,item[i].sub, &item[i].cat, &item[i].sec)>1)
    {
        printf("Term:%d\n", item[i].term);
        printf("Student ID:%d\n", item[i].sID);
        printf("LastName:%s\n,", item[i].lname);
        printf("FirstName:%s\n", item[i].fname);
        printf("Subject:%s\n", item[i].sub);
        printf("Category:%d\n", item[i].cat);
        printf("Section:%s\n", item[i].sec);
        i++;
    }
    

    最重要的变化是format的{​​{1}}。 fscanf表示在下一个%[\"]之前读取以下字符串。

    "
  2. 初始化1234,123456789, " Smith " , " Bob A. " , ABC , 123,= " A01 " | | | | | | | | | | | | | | | || | | | %d , %d , \" %[^\"] \", \" %[^\"] \",%[^,] , %d ,= \" %[^\"] \" item

  3. 实际上您可以尝试清除所有警告,编译器会提供非常有用的信息。不要忽视他们是一个很好的习惯。