简体   繁体   中英

C outputting “{” when reading from file

I have written this code using Xcode to read from a file:

int main (int argc, char *argv[])
{       
    FILE *fp = fopen("hello.rtf", "r");

    printf("%c\n", fgetc(fp));

    fclose(fp);

    if (fp == NULL)
    {
        printf("Could not open file!");
        return 1;
    }

    // insert code here...
    return 0;
}

The character that I get is "{" and it is not the first character in the file.

The RTF spec says that the first character in an RTF file should be { , so it seems that you are getting the expected result. Bear in mind that a word processing software will not show you the exact characters in the file, but it will show you the formatted text which has been described by the markup characters in the file.

To see the exact characters in the file you could output it with cat (POSIX) or type (DOS / Windows command prompt).

The if (fp == NULL) check should go immediately after the fopen line; by the end it's too late.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM