This tutorial will show you how to read and write files in C.
All file functions need stdio.h to work properly.
The first thing you need to know about is file pointers. File pointers are like any other pointer, but they point to a file. (Kind of obvious). You define a file pointer as follows:
In order to make the file pointer point to a file you use the fopen function. The function works as follows:Code:FILE *filepointer;
fopen returns a file pointer. It returns NULL if the file does not exist.Code:filepointer=fopen("filename", "mode");
fopen takes the first argument as the filename to open. It needs to be a string.
The second argument is the mode argument
Mode specifies what you want to do with the file.
Some modes are:
"r" - read the file
"w" - write the file
"a" - append to the file
"r+" - read and write to the file
"w+" - read and write, overwrite the file
"a+" - read and write, append
These modes will open files in text mode. Files opened in text mode have some bytes filtered out. If you want to open binary files use binary mode by adding a "b" to the mode. For example:
"rb" - read the file in binary mode
To read a character from a file, you use fgetc. It is like getchar, but for files.
It works like this:
fgetc returns the character that is read from the file as an integer.Code:character=fgetc(filepointer);
fgetc takes the file pointer as its only input.
It will automatically increment the pointer to read the next character.
fputc allows you to write a character to a file:
fputc takes an unsigned char as the first argument and the file pointer as the second argument.Code:fputc(character, filepointer);
fputc returns EOF on failure.
You can also use fprintf and fscanf. They work like printf and scanf, except the file pointer is the first argument. They work like this:
In order to close the file again, you must use fclose. It looks like this:Code:fprintf(filepointer, "Hello, World!\n"); //write hello world to the file fscanf(filepointer, "%d", integer); //read an integer from the file
fclose closes the file that filepointer points to.Code:fclose(filepointer);
If you want to print the contents of data.txt the code would look like this:
That's it! If you have questions, comments or suggestions feel free to post! +rep is very appreciated.Code:#include <stdio.h> int main() { FILE *filepointer; int character; filepointer=fopen("data.txt", "r"); //filepointer points to data.txt if (filepointer==NULL) { //error opening file returns NULL printf("Could not open data.txt!\n"); //error message return 1; //exit with failure } /* while character is not end of file */ while ((character=fgetc(filepointer)) != EOF) { putchar(character); //print the character } fclose(filepointer); //close the file return 0; //success }
Last edited by Guest; 01-17-2010 at 06:45 AM.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Nice tutorial. One thing I never fully took the time to understand is the other modes. Technically, aren't there 12 modes? r, w, a, r+, w+, a+ and then all those in binary mode? I never understood the difference between x, x+, and xb - could you elaborate on those?
Nicely done, I've already referred a new member to this tutorial.
+rep
short and sweet. +rep
This is good, well structured. +rep
Last edited by Guest; 11-04-2009 at 07:47 PM.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
+rep for the tut, you beggar![]()
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Great tutorial +rep
Last edited by debtboy; 11-10-2009 at 02:14 PM.
Thanks everyone![]()
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
very nice i learned something
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks