大家好我在打印包含 - >“需要帮助”的.txt文件的前两个字母/字符时出现问题。我想打印前两个字母 - >“ne”。我试着用CH [],但我不能确定,所以我改回了其运作部分:打印文件的第一个字母C
int main() {
char ch, file_name[2];
int i;
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r");
if(fp == NULL)
{
printf("Error while opening the file.\n");
exit(1);
}
printf("The contents of %s file are :\n", file_name);
while((ch = fgetc(fp)) != EOF)
printf("%c",ch);
fclose(fp);
return 0;
}
看看while循环。只有2个字符后,你将如何退出?数它们然后'打破;'。 – vsoftco
还有很多其他的东西......我的意思是,如果你的'file_name'多于一个字符...... –
不相关,但是'char ch,file_name [2];'这真的是你想要的吗? – John3136