gpt4 book ai didi

c - 读取数据文件时出现段错误

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

我正在尝试将字 rune 件读入 64 个字符的数组。文件读取发生在 main()

int main (int argc, char **argv)
{
FILE *fp;
char write_msg[BUFFER_SIZE];

fp = fopen("test.data", "r");

int i;
for ( i = 0; i < (BUFFER_SIZE-1); i++){
fscanf(fp, "%s\n", &write_msg[i]);
printf ("printing in the for loop");
}
fclose(fp);
printf("Starting the main().\n");
printf("in Reader, the msg is: %s\n",write_msg);

pid_t pid;
int fd[2];

我一直遇到段错误,我不确定自己在做什么

最佳答案

fscanf(fp, "%s\n", &write_msg[i]); 应该是 fscanf(fp, "%c\n", &write_msg[i]);,您不想将字符串扫描成字符。

并且 write_msg 应该以 '\0' 结尾,write_msg[BUFFER_SIZE-1]=0

关于c - 读取数据文件时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12667778/

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