¤ Home » Programming » C Tutorial » File Handling in C - Part 6 of 7

File Handling in C - Part 6 of 7

In order to further strengthen our knowledge of file handling, in this article I am providing you with a couple of sample C programs. Go through each of these programs in detail, write your own version of these programs and run them to develop a deeper understanding of C file handling functions.


C Program to find the size of a File

This program demonstrates the use of fseek() and ftell() functions to find the size of the file.

Give this source file a name such as findSize.c and then compile it to create the executable file as findSize.exe. You can then run the program by passing it the concerned file name as a command line argument, as -

C:\>findSize myfile.pdf

/*
 * Program to find the size of a file
 * Demonstrating use of functions - fseek, ftell
 * --------------------------------------------------------
 */
 #include <stdio.h> 

 main(int argc,char *argv[])
 {
   FILE *fp;
   char ch;
   double size = 0;
   void printResult(char *, double);
 
   fp = fopen(argv[1], "r");
   if(fp == NULL)
   {
	printf("\nERROR! File %s could not be open. ", argv[1]);
	printf("Check that file exists in same directory where this program is run.\n");
   }
   else
   {
	//Position the logical pointer in file to the end of file
	fseek(fp, 0, 2);
	
	//Now, tell the position of the pointer
	size = ftell(fp);

	fclose(fp);
	printResult(argv[1], size);
   }
 }

 
 //Function to print a meaningful file size with appropriate unit
 void printResult(char *fileName, double size)
 {
   if(size<1024)
   {
	printf("\nThe size of file %s is %d bytes\n", fileName, size);
   }
   else if(size<1000*1024)
   {
	size = size*1.0/1024;
	printf("\nThe size of file %s is %.2f Kb\n", fileName, size);
   }
   else
   {
	size = size*1.0/(1024*1024);
	printf("\nThe size of file %s is %.2f MB\n", fileName, size);
   }
 }


Exercises:

  1. Extend this program to handle very large files of size in GB.
  2. Write another C program to find the size of a file without using fseek and ftell.


C Program to Reverse the Contents of a File

Here is another program to demonstrate the use of fseek and ftell and other file handling functions. Note that this program will operate on text files only - it does not make sense to reverse the characters of a binary file.

/*
 * Program to Reverse the Contents of a File
 * -----------------------------------------
 */

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

 long getCharCount(FILE *);
 void fileCopy(char *, char *);

 main(int argc, char *argv[])
 {
   long nchars;
   char ch;
   FILE *fp1, *fp2;

   fp1 = fopen(argv[1], "r");
   if(fp1 == NULL)
   {
	printf("\nERROR! File %s could not be open. ", argv[1]);
	printf("Check that file exists in same directory where this program is run.\n");
	return;
   }

   fp2 = fopen("tempfile", "w");
   if(fp2 == NULL)
   {
	printf("\nERROR! Unable to create temporary file.\n");
	return;
   }

   nchars = getCharCount(fp1);

   //Position the logical pointer in file to the last character
   fseek(fp1, -1L, 2);

   while(nchars)
   {
	ch = fgetc(fp1); //this moves the logical pointer ahead be 1 char
	fputc(ch, fp2);

	//Shift logical back by 2 characters
	fseek(fp1, -2L, 1);

	nchars--;
   }
   fclose(fp1);
   fclose(fp2);

   //At this point we have the reversed content in tempfile
   //We copy this back to the original file
   fileCopy("tempfile", argv[1]);

   //Now, we must delete the tempfile
   //We use the standard C library function remove()
   remove("tempfile");

   printf("\nDONE! File %s successfully reversed.\n", argv[1]);
 }

 //Function to count total number of characters in the given file
 long getCharCount(FILE *fp) 
 {
   long count;
   fseek(fp, 0, 2); //position logical pointer to end of file
   count = ftell(fp);
   return count;
 }

 void fileCopy(char *src, char *destn)
 {
   FILE *fp1, *fp2;
   char ch; 
   fp1 = fopen(src, "r");
   fp2 = fopen(destn, "w");
   while((ch = fgetc(fp1)) != EOF) fputc(ch, fp2);
   fclose(fp1);
   fclose(fp2);
 }




Share:




comments powered by Disqus


Buy Domain & Hosting from the most reliable and trusted company - WebServicesWorldWide.com.

Looking to build a website?
Launch a 20 page website in 1 day at only Rs.200/month or US$ 3.19/month. Hosting & Email included.





About the Author

Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.



Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy