gpt4 book ai didi

找不到要在 c 中读取的 .txt 文件

转载 作者:行者123 更新时间:2023-11-30 17:28:06 25 4
gpt4 key购买 nike

我是 C 新手,我正在使用 OS X 10.9.5。我正在使用 Code::Blocks 并尝试了 Xcode,但使用它们似乎不可能打开和读取简单的 .txt 文件,除非我打开终端并自己找到该文件(使用 cd ...)。 .txt 和 .c 文件位于同一文件夹中。程序如何自动打开文本文件?这是我的代码:

#include <stdio.h>

int main () {

FILE *fp;
char name[50], city [50];
int age;

if((fp=fopen("text.txt","r"))) {
//printf("yes\n");
} else {
printf("doesnt open\n");
}

while (fscanf(fp,"%s\t%d\t%s\n",name, &age, city)!=EOF) {
printf("name=%s age=%d city=%s\n",name,age,city);
}

fclose (fp);


return 0;

}

最佳答案

您的工作目录与您期望的不同。如果您通过 IDE 运行应用程序,IDE 很可能会为您的应用程序设置一个工作目录。

获取当前工作目录(用于检查):How to get the current directory in a C program?

关于找不到要在 c 中读取的 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26129691/

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