Direkt zum Hauptbereich

Posts

Es werden Posts vom Oktober, 2009 angezeigt.

C++ Recurring Issues ¦ Reading a file

Opening Files for Reading Only Firstly, a text file is a file that you can open and read its contents visually - for example, C source files, .txt files, HTML etc - anything that looks "neat" in Notepad. A binary file is a file that is written in machine code - usually seen as a mass of weird characters in Notepad! Examples are bitmaps, executables etc. For these tutorials, we're going to be looking at standard text files. If you wanted to open a binary file, simply put a b at the end of the second argument of fopen . To open a text file for reading only, pass "r" as the second argument of fopen, as demonstrated in this example: #include int main() { FILE *file; / * declare a FILE pointer */ file = fopen("data/hello.txt", "r"); /* open a text file for reading */ if (file==NULL) { printf("Error: can't open file.\n"); /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */ return 1; } else { printf("File opened. Now c