帮助在 Xcode 上用 C 语言打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2054846/
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
Help with opening a file in C on Xcode
提问by Crystal
I'm trying to open a simple .rtf file called test in C. I'm using Xcode. My code is:
我正在尝试在 C 中打开一个名为 test 的简单 .rtf 文件。我正在使用 Xcode。我的代码是:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, const char * argv[]) {
FILE *filePtr;
filePtr = fopen("test.rtf", "r");
if (filePtr == NULL) {
fprintf(stderr, "Can't open \"test\"\n");
exit(EXIT_FAILURE);
}
else {
printf("File open successful\n");
int x;
/* read one character at a time until EOF is reached */
while ((x = fgetc(filePtr)) != EOF) {
printf("%c", x);
}
}
fclose(filePtr);
return 0;
}
I have the test.rtf file in the same directory as my Xcode.proj directory. My output is "File open successful", however I do not get anything read from the file. Am I doing this right? Thanks.
我的 test.rtf 文件与我的 Xcode.proj 目录位于同一目录中。我的输出是“文件打开成功”,但是我没有从文件中读取任何内容。我这样做对吗?谢谢。
回答by paxdiablo
There's nothing wrong with that code at all. I tested it (albeit not in Xcode) with a file and the transcript was:
这段代码没有任何问题。我用一个文件测试了它(虽然不是在 Xcode 中),成绩单是:
pax> echo hello >test.rtf
pax> ./qq.exe
File open successful
hello
So the obvious think to ask is what happens when you examine test.rtf? Does it actually have any content? Because, when I do:
所以显而易见的问题是当你检查 test.rtf 时会发生什么?它实际上有任何内容吗?因为,当我这样做时:
pax> rm test.rtf ; touch test.rtf
pax> ./qq.exe
File open successful
I get the same behaviour you observe.
我得到了你观察到的相同行为。
Also try renaming it to test2.rtf temporarily and make sure you get the error. It's possible it may be opening a different copy of the file than what you think (this often happens in Visual C since the directory the program runs in is not always what developers think at first).
也尝试将其暂时重命名为 test2.rtf 并确保您收到错误。它可能打开了与您想象的不同的文件副本(这在 Visual C 中经常发生,因为程序运行所在的目录并不总是开发人员最初想到的)。
回答by Mr. Berna
I can think of two things that could cause this problem. Either there is an error when calling fgetc, or you are getting output that you don't recognize.
我可以想到可能导致此问题的两件事。调用 fgetc 时出现错误,或者您收到无法识别的输出。
fgetc() will return EOF when the end of the file is reached, or an error occurs. To determine if it's an error, just after your while loop try:
fgetc() 将在到达文件末尾或发生错误时返回 EOF。要确定它是否是错误,请在您的 while 循环之后尝试:
if (ferror(filePtr) != 0) printf("error: %d.\n", errno);
if (ferror(filePtr) != 0) printf("error: %d.\n", errno);
A .rtf file is not a plain text file. It likely contains a bunch of formatting information. You are expecting to see "Hello . . . ". but what you may actually see is something like:
.rtf 文件不是纯文本文件。它可能包含一堆格式信息。你期待看到“你好……”。但你可能实际看到的是这样的:
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf250 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \margl1440\margr1440\vieww9000\viewh8400\viewkind0 \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040 \f0\fs24 \cf0 Hello . . .
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf250 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \margl1444\00view10w7h8\margl tx1440\tx2160\tx2880\tx3600\tx4320\tx5040 \f0\fs24 \cf0 你好。. .
And you are just assuming that is GDB output, not your program's output.
而且您只是假设那是 GDB 输出,而不是您程序的输出。
回答by Alok Singhal
Based upon your recent comments, I think you have an empty file test.rtf
in the directory your program is run in, and your real test.rtf
file is in some other directory. Maybe your fopen()
call at some point was fopen("test.rtf", "w");
instead of fopen("test.rtf", "r");
, and you later modified it.
根据您最近的评论,我认为您test.rtf
的程序运行所在的目录中有一个空文件,而您的真实test.rtf
文件位于其他目录中。也许您fopen()
在某个时候的调用fopen("test.rtf", "w");
不是fopen("test.rtf", "r");
,而您后来对其进行了修改。
To see the directory your program is running in, add the following to your program after the FILE *filePtr;
line:
要查看您的程序运行所在的目录,请将以下内容添加到您的程序的FILE *filePtr;
行后:
char pwd[512];
if (getcwd(pwd, sizeof pwd) != -1)
printf("In directory %s\n", pwd);
else
fprintf(stderr, "Need bigger buffer, change '512' above\n");
Then, you can open a terminal, do cd <directory>
, and test for yourself if the file you want is the file your program is opening.
然后,您可以打开一个终端,执行cd <directory>
并自行测试您想要的文件是否是您的程序正在打开的文件。
回答by Peter Zich
You probably want this file to be plain text, not rich text. Rich text has a lot of formatting encoded into the file.
您可能希望此文件是纯文本,而不是富文本。富文本有很多格式编码到文件中。
回答by wallyk
It looks right.
看起来不错。
As for the lack of output, two possibilities:
至于输出不足,有两种可能:
- Are you sure the file has some content? Maybe
ls -l test.rtf
ordir test.rft
- Possibly it has some control characters which cause the terminal to which it is written to suppress output.
- 你确定这个文件有一些内容吗?也许
ls -l test.rtf
或dir test.rft
- 可能它有一些控制字符导致写入它的终端抑制输出。
回答by Chris Long
Try moving test.rtf
to your build directory. If your project is named MyProject
, move it to MyProject/build/Debug/
.
尝试移动test.rtf
到您的构建目录。如果您的项目已命名MyProject
,请将其移至MyProject/build/Debug/
.