gpt4 book ai didi

c - 如何从文本文件中读取文本并将每个字符存储在数组中

转载 作者:行者123 更新时间:2023-12-04 05:11:17 26 4
gpt4 key购买 nike

我正在尝试从 duom.txt 文件中读取文本并将每个字符存储到数组中。
但我没有得到正确的答案。
我的代码有什么问题?

# include <stdio.h>
# include <stdlib.h>
int main()
{
FILE *in;
char ch,str[100],cw;
int j,i = 0;

in=fopen("duom.txt","r");


if(in){
while(!feof(in)){
ch=getc(in);
str[i] = ch;
i++;
}
}

for(j=0;j<i;j++){
printf("%c",str[i]);
}
printf("\n");
fclose(in);


system("pause");
return 0;
}

duom.txt 文件:
My name is Lukas

最佳答案

  • fgetc() 返回 int所以类型 ch应该是 int .
  • feof() 告诉你是否读过文件末尾。这意味着您的 while 循环将比预期多执行一次。
  • 关于c - 如何从文本文件中读取文本并将每个字符存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14911803/

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