Reputation: 29458
I'm trying to open a simple .rtf file called test in C. I'm using Xcode. My code is:
#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.
Upvotes: 0
Views: 6833
Reputation: 512
You probably want this file to be plain text, not rich text. Rich text has a lot of formatting encoded into the file.
Upvotes: 0
Reputation: 96131
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.
To see the directory your program is running in, add the following to your program after the FILE *filePtr;
line:
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.
Upvotes: 0
Reputation: 10645
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() 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:
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:
{\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 . . .
And you are just assuming that is GDB output, not your program's output.
Upvotes: 0
Reputation: 881403
There's nothing wrong with that code at all. I tested it (albeit not in Xcode) with a file and the transcript was:
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:
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).
Upvotes: 1
Reputation: 3074
Try moving test.rtf
to your build directory. If your project is named MyProject
, move it to MyProject/build/Debug/
.
Upvotes: 0
Reputation: 57774
It looks right.
As for the lack of output, two possibilities:
ls -l test.rtf
or dir test.rft
Upvotes: 0