tags:

views:

300

answers:

6

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.

A: 

It looks right.

As for the lack of output, two possibilities:

  • Are you sure the file has some content? Maybe ls -l test.rtf or dir test.rft
  • Possibly it has some control characters which cause the terminal to which it is written to suppress output.
wallyk
A: 

Try moving test.rtf to your build directory. If your project is named MyProject, move it to MyProject/build/Debug/.

Chris Long
Unlikely to be the cause. OP says `File open successful` appears.
wallyk
Yes I tried moving around my file.
Crystal
+1  A: 

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).

paxdiablo
I changed "test.rtf" in the filePtr = fopen("test.rtf", "r") to "t.rtf" to make sure I get the error printed when the file does not open. The .rtf file I have I just put some random text in there like "hello (newline) does this (newline) work".
Crystal
The only thing I can think of now is to test the return value of the `printf()` call!
Alok
What happens when you change 'printf("%c", x);' to 'fprintf(stderr,"%x\n", x);'? If you got the error okay when the file wasn't there, minimise the differences between the output calls - both use stderr, both output something with newlines.
paxdiablo
Same output. File open successful.
Crystal
A: 

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.

Mr. Berna
what is GDB? So I tried creating a new .txt file instead and I keep getting "Can't open test" even though the file is in the same place as the old .rtf file was. I tried a few different names and put the file in the debug folder along with trying it in the same folder as my Xcode.proj, but this doesn't seem to work either. Any other things to try out? Thanks.
Crystal
GDB is the GNU debugger. Xcode uses GDB when running a Debug build of a program. Most of the output you see in Xcode's console is GDB output. Did you change the file name to test.txt (or whatever you named the .txt file) in your code?
Mr. Berna
Yes. I created a new text (.txt) file and for some reason it wouldn't open my file and would continue outputting "Cant' open file".
Crystal
The next step is to find out why it can't open the file. fopen should set errno on an error, so output errno. Change the line 'fprintf(stderr, "Can't open \"test\"\n");' to 'fprintf(stderr, "Can't open \"test\": %d.\n", errno);'. Then look up what the error number means to find out what needs to be fixed. By the way, errno.h must be included to use errno.
Mr. Berna
A: 

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.

Alok
A: 

You probably want this file to be plain text, not rich text. Rich text has a lot of formatting encoded into the file.

Peter Zich