使用fprintf和fscanf时出错

我有一个存档results.csv ,我需要读取此存档的第一行并在output.txt上打印出来。 不知怎的,它在所有东西之后打印随机字符,我无法弄清楚出了什么问题。

命令: ac results.csv

第一行: date,home_team,away_team,home_score,away_score,tournament,city,country,neutral

output.txt: date,home_team,away_team,home_score,away_score,tournament,city,country,neutral,(!£,(!£,(!£,(!£,(!£,@,£,(!£,(!£

 #include  #include  #include  #include  typedef struct { char *line1; char *line1a; char *line1b; char *team1; char *team2; char *reason; char *city; char *country; char *neutral_field; }data; void open_input(char *argv[], FILE **input) { if((*input=fopen(argv[1], "r")) == NULL) { printf("%s not found\n", argv[1]); exit(1); } } void open_output(char *string, FILE **output) { if((*output=fopen(string, "w")) == NULL) { printf("%s not found\n", string); exit(1); } } void alloc_data(data *d, int size) { d->line1 = (char*)malloc(4*sizeof(char)); d->team1 = (char*)malloc(9*sizeof(char)); d->team2 = (char*)malloc(9*sizeof(char)); d->line1a = (char*)malloc(10*sizeof(char)); d->line1b = (char*)malloc(10*sizeof(char)); d->reason = (char*)malloc(10*sizeof(char)); d->city = (char*)malloc(4*sizeof(char)); d->country = (char*)malloc(7*sizeof(char)); d->neutral_field = (char*)malloc(7*sizeof(char)); } void store(data *d, FILE *input, FILE **output) { fscanf(input, "%s,%s,%s,%s,%s,%s,%s,%s,%s", d[0].line1, d[0].team1, d[0].team2, d[0].line1a, d[0].line1b, d[0].reason, d[0].city, d[0].country, d[0].neutral_field ); fprintf(*output, "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", d[0].line1, d[0].team1, d[0].team2, d[0].line1a, d[0].line1b, d[0].reason, d[0].city, d[0].country, d[0].neutral_field ); } int main(int argc, char *argv[]) { FILE *input; FILE *output; char *string = "output.txt"; int size = 1000; open_input(argv, &input); open_output(string, &output); data *d; d = (data*)malloc(size*sizeof(data)); alloc_data(d, size); store(d, input, &output); free(d); return 0; } 

fscanf(input, "%s,%s,%s,%s,%s,%s,%s,%s,%s", d[0].line1, d[0].team1,...

上面的代码试图将整行读入d[0].line1 ,这会导致缓冲区溢出。 team1和其他将包含未初始化的数据。

您必须按如下方式更改fscanf

 fscanf(input, "%3[^ ,\n\t],%9[^ ,\n\t],... 

其中3是4 – 1,而4是d[0].line1的大小

或者你可以使用strtok

 #include  #include  #include  void store(FILE *input, FILE *output) { char buf[500]; while(fgets(buf, sizeof(buf), input)) { //strip end-of-line from `buf` if(strlen(buf)) if(buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = 0; //tokenize with strtok char *token = strtok(buf, ","); while(token) { fprintf(output, "%s", token); token = strtok(NULL, ","); } fprintf(output, "\n"); } } int main(int argc, char *argv[]) { FILE *input = fopen("input.txt", "r"); FILE *output = fopen("output.txt", "w"); store(input, output); return 0; } 

使用上面的代码,您不需要额外的结构。


如果您确实使用数据结构,则必须更加小心。 您似乎正在尝试创建一个包含1000个data的数组,但以下只创建一个超大指针,而不是data数组

 int size = 1000; data *d; d = (data*)malloc(size*sizeof(data)); alloc_data(d, size); 

另外,对于每个malloc ,应该有相应的free

您的缓冲区不足以容纳终止NUL字节。 scanf存储NUL字节(超出缓冲区),但真正拥有该字节的对象可能会覆盖它,因此当printf查找NUL时,它直到内存中才会找到它。

缓冲区溢出是一个比你看到的更大的问题,谁知道你没有留出空间的NUL字节是什么对象粉碎? 当您读取标题拼写略有不同的数据文件时会发生什么? 突然之间,您的硬编码分配大小将比它们已经更加错误。