C Video Tutorial 11

C File IO ExampleIn this part of the C Video Tutorial I will provide C File IO examples. I’ll cover C Text File IO in this tutorial and the Binary File IO functions in the next part.

I specifically cover the following topics: FILE Data type, fopen(), fputs(), fseek(), ftell(), fgets(), fclose(), fscanf(), puts(), fprintf(), Different Ways to Access Files, and more. All of the code used is available below the video.

If you like videos like this, it helps to tell Google+ with a click here

Code From the Video

CTutorial11.c

#include <stdio.h>
#include <stdlib.h>

// C provides numerous methods for working with files
// A file is just a block of memory with a name
// The file can be stored across many blocks in memory, 
// but the C compiler acts as if it is in sequence.

// In this tutorial I will cover how to work with files
// if we represent them as text files. next time I'll cover how
// to use Binary File I/O

/*
int main(){

	int randomNumber;
	
	// Will store the pointer to the file
	
	FILE *pFile;
	
	// Opens a text file so we can write data to it.
	// It returns a pointer to the file
	// w - If a file doesn't exist create it. If it does overwrite it
	// a - Open or create text file and append to original content
	// r - Open a file to read from it
	// r+ - Open for reading and writing
	// w+ - Open or create, delete original and read and write
	// a+ - Open or create, append for writing and reading
	
	pFile = fopen("randomnumbers.txt", "w");
	
	// If the file wasn't opened a 1 is returned from fopen()
	
	if(!pFile){
	
		printf("Error : Couldn't Write to File\n");
		
		return 1;
	
	}
	
	// Print 10 random numbers to the file
	
	for(int i = 0; i < 10; i++){
	
		randomNumber = rand() % 100;
		
		// fprintf() prints to the file supplied just like
		// printf() prints to the screen
		
		fprintf(pFile, "%d\n", randomNumber);
	
	}
	
	printf("Success Writing to File\n");
	
	// Closes the text file
	// This must be done or eventually the program will run 
	// out of file handlers or memory
	

	if(fclose(pFile) != 0)
		printf("Error : File Not Closed\n");

	return 0;
}
*/

int main(){

	// Defining that we only expect there to be a maximum of 1,000
	// characters per lines read.

	char buffer[1000];
	
	FILE *pFile;
	
	// Opens the file for reading
	
	pFile = fopen("randomnumbers.txt", "r");
	
	// fopen returns 1 if an error occurred 
	
	if(!pFile){
	
		printf("Error : Couldn't Read the File\n");
		
		return 1;
	
	}
	
	// fgets() reads up to 1,000 characters per line until
	// fgets() returns the value of NULL when it reaches 
	// the end of the file.
	
	// while(fgets(buffer, 1000, pFile) != NULL){
	
	//	printf("%s", buffer);
	
	// }
	
	
	// You can also read from a file using fscanf
	// You pass the file, data type to read and the buffer
	// to store it in. You can check for valid data because 
	// fscanf() returns a number other then 1 if it isn't 
	// a string
	
	
	while(fscanf(pFile, "%s", buffer) == 1){
	
		// Puts outputs the string plus a newline.
		// Returns a nonnegative integer if it was successful
		// and EOF if there was an error
	
		puts(buffer);
	
	}
	
	
	printf("Success Reading from File\n");
	
	// Closes the text file
	
	if(fclose(pFile) != 0)
		printf("Error : File Not Closed\n");

	return 0;
}

CTutorial11_2.c

#include <stdio.h>
#include <stdlib.h>

int main(){

	FILE * pFile;
	
	pFile = fopen("randomwords.txt", "r+");
	
	char buffer[1000];
	
	// If the file wasn't opened a 1 is returned from fopen()
	
	if(!pFile){
	
		printf("Error : Couldn't Write to File\n");
		
		return 1;
	
	}
	
	fputs("Messing With Strings", pFile);
	
	// Move 12 spaces from the beginning of the file
	// SEEK_SET - Move starting from the beginning of the file
	// SEEK_CUR - Move based off of the current position in the file
	// SEEK_END - Move based off of starting at the end of the file
	
	fseek(pFile, 12, SEEK_SET);
	
	// Overwrite characters starting at char number 12
	
	fputs(" Files  ", pFile);
	
	printf("Success Writing to File\n");
	
	// Move back to the beginning of the file
	
	fseek(pFile, 0, SEEK_SET);
	
	// 2 : Move to the end of the file
	
	fseek(pFile, 0, SEEK_END);
	
	// ftell() tells you how far you are currently from the
	// beginning of the file
	
	long numberOfBytes = ftell(pFile);
	
	printf("Number of Bytes in File : %d\n", numberOfBytes);
	
	// 3 : This moves backwards 20 bytes in the file
	
	fseek(pFile, -20, SEEK_CUR);
	
	while(fgets(buffer, 1000, pFile) != NULL){
	
		printf("%s", buffer);
	
	}
	
	printf("\n");
	
	// Closes the text file
	// This must be done or eventually the program will run 
	// out of file handlers or memory
	
	if(fclose(pFile) != 0)
		printf("Error : File Not Closed\n");

	return 0;
}

2 Responses to “C Video Tutorial 11”

  1. Paul says:

    Thank you for the tutorials in C!

    I enjoyed your Java lessons – they helped me quite a bit in school. I am now taking a Discrete Structures course where I must implement assignments in the C programming language and was pleasantly surprised to find these videos from a trusted source.

    Point of clarification:
    In the commented section of the first main function, I notice it states that fopen() returns a ‘1’ if it is unsuccessful. Is this correct? Other sources state that it returns NULL or a ‘0’ if it is unsuccessful.

    Thank you in advance!

Leave a Reply

Your email address will not be published.

Google+