xcode 在xcode的c编程中打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7121752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a file in c programming in xcode
提问by noddy
I am trying to write a file to open a file and read the contents of that file in C. I am using xcode but the file pointer returns a value null.
我正在尝试编写一个文件来打开一个文件并在 C 中读取该文件的内容。我使用的是 xcode,但文件指针返回一个值 null。
int main(int argc, char** argv)
{
FILE *fp;
fp=fopen("input.txt","r");
if (fp==NULL)
printf("error");
}
This shows error as the output. Could someone help me to find the right place to put the file input.txt in the project?
这将错误显示为输出。有人可以帮我找到将文件 input.txt 放在项目中的正确位置吗?
回答by Ernest Friedman-Hill
The file would need to go in the "current directory" where the program is launched. This is configurable. In the "Groups and Files" pane, expand "Executables" and pick your executable. Press Command-I to open the info window. Near the bottom of the "General" tab, you can select the current directory for when the application is launched.
该文件需要进入程序启动的“当前目录”。这是可配置的。在“组和文件”窗格中,展开“可执行文件”并选择您的可执行文件。按 Command-I 打开信息窗口。在“常规”选项卡底部附近,您可以选择应用程序启动时的当前目录。
回答by vivek241
In your file list on the left right click on your product (executable) and click on "Show in Finder". Paste the file you want to open in your program there.
在左侧的文件列表中,右键单击您的产品(可执行文件),然后单击“在 Finder 中显示”。将要在程序中打开的文件粘贴到那里。
回答by Jon
This is somewhat related, but if you want to open a file in iOS, then get the file path via the following:
这有点相关,但如果你想在 iOS 中打开一个文件,那么通过以下方式获取文件路径:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Jon" ofType:@"CSV"];
FILE *file = fopen([filePath cStringUsingEncoding:NSUTF8StringEncoding], "r");
回答by Karoly Horvath
The file is not in the working directory or you don't have the right to read it.
该文件不在工作目录中,或者您无权阅读它。
回答by noddy
i got the answer .I needed to store the file in the debug folder because xcode runs the file from the debug folder which is
我得到了答案。我需要将文件存储在调试文件夹中,因为 xcode 从调试文件夹运行文件