我在哪里设置Xcode IDE属性来读取数据文件以避免没有这样的文件或目录 [英] Where do I set the Xcode IDE property to read a data file to avoid No such file or directory

查看:155
本文介绍了我在哪里设置Xcode IDE属性来读取数据文件以避免没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是代码问题,而是Xcode IDE问题。
对于那些为代码分析感到自豪的人来说,这很好,但在这种情况下,请尝试关注上面标题中的问题,而不是代码。

This is not a code question but an Xcode IDE question. For those who pride themselves in code analysis, that's good, but in this case, please try to focus on the question in the Title above not the code.

在Xcode IDE中,我创建了一个新项目并选择了一个命令行工具来创建ReadAFile。
然后我在main.c中输入了一些简单的代码来读取和显示Data.txt的内容。
然后我选择File,New,File,并选择Empty(对于空文档)并将其命名为Data.txt然后我输入一个句子然后保存文件。
在Xcode中,当我选择Product,Run时,我收到以下错误:

In the Xcode IDE I created a new project and selected a "Command Line Tool" creating ReadAFile. I then entered some simple code into main.c to read and display the contents of "Data.txt". I then selected File, New, File, and selected Empty (For an empty document) and named it Data.txt and then I typed out a sentence then saved the file. In Xcode, when I choose Product, Run, I get the following error:

Error while opening the file.
: No such file or directory
Program ended with exit code: 1

我在哪里设置Xcode IDE属性,以便它知道我的数据文件在哪里,以避免出现没有这样的文件或目录错误?

Where do I set the Xcode IDE property so that it knows where my data file is to avoid getting the "No such file or directory" error?

当我进入非常将同样的代码写入ReadAFile.c并在终端提示符下使用vim输出完全相同的文件Data.txt,保存并运行make ReadAFile,代码执行正常。

When I enter the very same code into ReadAFile.c and I type out the very same file "Data.txt" at a terminal prompt using vim, save it, and run make ReadAFile, the code executes properly.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[]) {
    char ch, file_name[25];
    FILE *fp;
    strcpy(file_name,"Data.txt");
    fp = fopen(file_name,"r");
    if( fp == NULL ) {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }
    printf("The contents of %s file are :\n", file_name);
    while( ( ch = fgetc(fp) ) != EOF )
        printf("%c",ch);
    fclose(fp);
    return 0;
}


推荐答案

这是个好问题..我有一个临时的,解决方法,回答...

我尊重你的要求,专注于问题而不是你的代码。

作为一个快速的临时答案,唯一回答我现在为您修改您的代码,现在,如果您只是在调试代码和数据文件。 我知道对您的文件位置进行硬编码是一个禁忌 ,但出于调试目的 ,以下内容将帮助您解决这个问题 > 即可。您可以尝试将目录位置与数据文件一起硬编码,以便代码可以找到它。例如,假设Mac上的Users目录是jerryletter,您可以将Data.txt文件复制到Users目录〜/ jerryletter。然后可以修改代码的一行,如下所示:

That's a good question... I have a temporary, work around, answer...
I do respect your request to focusing on the question rather than your code.
As a quick temporary answer, the only answer I have for you at this point is to modify your code, for now, if you are only debugging your code along with your data file. I know hard coding your file locations is a no-no but for debugging purposes the following will get you over this bump in your road. You can try to hard code the directory location along with your data file so that your code can find it. For example, let's say that your Users directory on your Mac is jerryletter, you could copy your Data.txt file to your Users directory ~/jerryletter. Then could modify just one line of your code like the following:

"strcpy(file_name,"/Users/jerryletter/Data.txt");"

DougPan.com

DougPan.com

这篇关于我在哪里设置Xcode IDE属性来读取数据文件以避免没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆