我一直通过 Bootcamp 使用 Visual Studio,今天我决定尝试 XCode。 然而,当我正在测试 C 语言的基本 I/O 时,就出现了问题。
#include <stdio.h>
int main(void)
{
FILE *ifp,*ofp;
char name[20];
int age;
double height;
int res;
ifp=fopen("a.txt","r");
if(ifp==NULL)
{
printf("input file open error!\n");
return 1;
}
ofp=fopen("b.txt","w");
if(ofp==NULL)
{
printf("output file open error!\n");
return 1;
}
while(1)
{
res=fscanf(ifp,"%s %d %lf",name,&age,&height);
if(res==EOF)break;
fprintf(ofp,"%.1lf %d %s\n",height,age,name);
}
fclose(ifp);
fclose(ofp);
return 0;
}
据我所知,这在 Visual Studio 中完全正常。 但是,如果我在 XCode 中尝试此代码,结果如下所示:
b.txt中打印出的结果:
a.txt的原始内容如下所示:
Lewis 28 182.4 James 53 172.5 Sarah 14 164.3
最佳答案
我使用了文本编辑器,这是 mac 上的一个基本程序。为了将文件保存为纯txt文件而不是导致上述问题的rtf文件,您需要转到工具栏上的格式部分并选择更改以进行简单测试,或shift+command+T进行快捷方式。
关于c - 在 Xcode 中使用 C 的文件 I/O 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45279131/