c - 在 Xcode 中使用 C 的文件 I/O 时遇到问题

标签 c xcode

我一直通过 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中打印出的结果: result printed out in 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/

相关文章:

c - 'a'是不是代表1在 "ch = input[c] - ' a'”?

ios - 在 iPad 10.5 英寸模拟器中运行应用程序?

objective-c - XCode:两个目标,两个主要文件?

c - 为构建你自己的 Lisp 书滚动一个解析器?

c - 如何在不使用字符串的情况下计算单词总数?

c++ - 语句内的等号/赋值运算符

ios - 在以前的版本中为应用程序创建快捷方式会崩溃

ios - Swift:通过 NSNotificationCenter 的键盘观察器不起作用

ios - 更改选项卡栏中选项卡的目标 View Controller

c - 如何在函数中使用 grep?