File I/O

Requires the preprocessor directive:

 

#include <stdio.h>

 

Requires a file pointer:

 

FILE *file-variable-name ; // sets up a pointer for the file to be opened

 

FILE is provided from stdio.h

 

When the pointer is declared the target file can be opened and assigned to the file-variable-name

 

file-variable-name = fopen( target-file-name, mode ) ;

Mode Description
r Opens an existing text file for reading
w Opens or creates a text file for writing. If file exists, truncate to zero length, else create
a Opens or creates a text file for writing from end of file, appending to any existing data
rb Opens an existing binary file for reading
wb Opens or creates a binary file for writing. If file exists, truncate to zero length, else create
ab Opens or creates a binary file for writing from end of file, appending to any existing data

 

FILE *filePointer ; //declares a variable pointer called filePointer

 

filePointer = fopen( "C:target-file.txt", "w" ) ; //target-file.txt is now open in write mode, and assigned to filePointer

 

Data can now be written to the file via filePointer:

 

fprintf( filePointer, "Here's some data being written to a text file" ) ;

 

The file must then be closed:

 

fclose( filePointer ) ;

 

#include <stdio.h>

int main(){

	FILE *filePointer;

	filePointer=(fopen("C:target-file.txt","w")); //note having to escape the backslash

	fprintf(filePointer,"Here's some data being written to a text file.n");

	fclose(filePointer);

	return 0;
}

Compile & Run produces a file called target-file.txt, that contains the text "Here's some data being written to a text file."

 


 

fgets() and fputs() can also be used to read/write data to/from the Standard Streams, or a file

 

fgets(name, size-of-name,file) //where file could be stdin

fputs(string, file) //where file could be stdout

 

This simple program reads text entered from the keyboard and prints it back out on the screen:

#include <stdio.h>
#include <stddef.h>

int main()
{
  char myText[256]; //create empty char array

  fgets(myText, 256, stdin); //get string from stdin, store in myText char array

  fputs(myText, stdout); //write data in myText to stdout

  return 0;
}

 

A variation on the above, using a file to store the data in:

#include <stdio.h>

int main ()
{
	FILE * filePointer; //create a file pointer

	char myText [256]; //create empty char array

	printf("Enter sentence to append: "); //ask for input

	fgets (myText, 255, stdin); //get string from stdin, store in myText char array

	filePointer = fopen("TextFile.txt", "a"); //open target file in append mode

	fputs(myText, filePointer); //write data in myText char array to file via filePointer

	fclose(filePointer); //close the file

	return 0;
}

 

Another variation, reading data from a file and displaying it on the screen:

#include <stdio.h>
#include <stddef.h>
#include <errno.h>

int main ()
{
	FILE * filePointer ; //create a file pointer

	char myText [256] ; //create empty char array

	filePointer = fopen("TextFile.txt", "r") ; //open target file in read mode

	if (filePointer == NULL) { //check file can be opened

		perror("Error opening file"); //if not print error message

	} else { //if file opened carry on

		if (fgets(myText, 255, filePointer) != NULL) {//read contents of file into char array

			fputs(myText, stdout); //print char array to stdout

			fclose(filePointer); //close the file
		}
	}

	return 0;
}

 


*Note: the <stdio.h> fscanf() function has not been explained since it felt to be generally unreliable seeing that it can only read preceding characters up to the first space character, any characters after the space are ignored and it is for that reason that it is thought to be inappropriate for use in a general purpose robust file reader.

Leave a Reply