gpt4 book ai didi

c - 使用 fprintf 和 fscanf 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:06 25 4
gpt4 key购买 nike

我有一个存档 results.csv,我需要读取这个存档的第一行并将其打印到 output.txt 上。它以某种方式在所有内容之后打印随机字符,我无法弄清楚出了什么问题。

命令:a.c 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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>


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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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

关于c - 使用 fprintf 和 fscanf 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50743661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com