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 closing it...\n");
fclose(file);
return 0;
}
}
Other Options When Opening Files
There are 4 other options I'd like to mention:
"a" lets you open a text file for appending - i.e. add data to the end of the current text.
"r+" will open a text file to read from or write to.
"w+" will create a text file to read from or write to.
"a+" will either create or open a text file for appending.
Add a "b" to the end if you want to use binary files instead of text files, like this:
"rb", "wb", "ab", "r+b", "w+b", "a+b".
Using fgetc To Read Characters
First, we will look at a function that allows us to read one character at a time.
fgetc requires one argument - a stream. This could either be your FILE pointer OR stdin. Writing fgetc(stdin) is the equivalent of writing getc(). We're going to be passing a FILE pointer to it.
fgetc returns the int that denotes a character from the file. For example, if the first character in the file was "A", fgetc would return 65.
After returning an int, the FILE pointer is moved to point to the next character. Should the pointer be moved beyond the last character, fgetc returns EOF - the END OF FILE constant.
We usually use EOF to determine when we've finished reading from a file.
For the next example, I created a text file called numbers.txt and placed it in the same directory as my C program. Here are its contents:
one
two
three
four
five
Here's a demonstration the use of fgetc:
#include
int main() {
char c; /* declare a char variable */
FILE *file; /* declare a FILE pointer */
file = fopen("numbers.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 successfully. Contents:\n\n");
while(1) { /* keep looping... */
c = fgetc(file);
if(c!=EOF) {
printf("%c", c);
/* print the file one character at a time */
}
else {
break; /* ...break when EOF is reached */
}
}
printf("\n\nNow closing file...\n");
fclose(file);
return 0;
}
}
Kommentare
Kommentar veröffentlichen
Show me Your opinion or leave me a hint if You found a bug or typo. I appreciate any help and feedback.
Sag Deine Meinung oder gib mir einen Tipp, wenn sich ein Fehler auf der Seite eingeschlichen hat. Ich schätze jede Form von Rückmeldung.