Reading and Writing Files in C

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
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:
Code:
FILE *filepointer;
In order to make the file pointer point to a file you use the fopen function. The function works as follows:
Code:
filepointer=fopen("filename", "mode");
fopen returns a file pointer. It returns NULL if the file does not exist.
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:
Code:
character=fgetc(filepointer);
fgetc returns the character that is read from the file as an integer.
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:
Code:
fputc(character, filepointer);
fputc takes an unsigned char as the first argument and the file pointer as the second argument.
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:
Code:
fprintf(filepointer, "Hello, World!\n"); //write hello world to the file
fscanf(filepointer, "%d", integer); //read an integer from the file
In order to close the file again, you must use fclose. It looks like this:
Code:
fclose(filepointer);
fclose closes the file that filepointer points to.

If you want to print the contents of data.txt the code would look like this:
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
}

That's it! If you have questions, comments or suggestions feel free to post! +rep is very appreciated.

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top