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

File Handling in C - Part 4 of 7

String I/O functions - fscanf, fprintf

The use of functions fscanf & fprintf can be best understood by looking at an example program. In the program below, we have created two FILE pointers and both are referring to the same file but in different modes. fprintf() function directly writes into the file, while fscanf() reads from the file, which can then be printed on console using standard printf() function.

/*
 Program to Read and Write from File using fscanf() and fprintf()
 ---------------------------------------------------------------
*/

#include <stdio.h> 

struct emp
{
  char name[21];
  int age;
};

main()
{
  FILE *fp1, *fp2;
  struct emp e;

  fp1 = fopen("one.txt", "a");
  printf("Enter Name and Age:\n");
  scanf("%s %d", e.name, &e.age);

  fprintf(fp1, "%s %d", e.name, e.age);
  fclose(fp1);

  printf("\n\nPrinting contents of file:\n");
  fp2 = fopen("one.txt", "r");
  do
  {
    fscanf(fp2, "%s %d", e.name, &e.age);
    printf("%s %d\n", e.name, e.age);
  } while(!feof(fp2));
  fclose(fp2);
}

Note that in the above program we are opening the file in append mode (a) for writing.


Difference between Append and Write Mode

Write (w) mode and Append (a) mode, while opening a file are almost the same. Both are used to write in a file. In both the modes, new file is created if it does not already exist. The only difference they have is, when you open a file in the write mode, the file is reset, resulting in deletion of any data already present in the file. While in append mode this will not happen. Append mode is used to append or add data to the existing data of file, if any. Hence, when you open a file in Append (a) mode, the cursor is positioned at the end of the present data in the file.


Exercises:

1. The skeletal outline of a C program is shown below. Introduce appropriate statements to read the values of the variables x, y and z from the file some_fil.inf and display them on the screen.

  #include<stdio.h>
  main(int argc, char *argv[])
  {
	FILE *fp;
	int x;
	float y;
	char z;
	fp = fopen("some_fil.inf", "r");
	. . .
	. . .
	fclose(fp);
  }

2. The following program after reading a stream of characters from an existing file, displays it on the screen and also writes onto a new file. Suppose the compiled version of this program is stored in a file called copy.exe; the existing data is stored in a file called old_file and the new file to be created is new_file. Show how the command line would be written to execute this program.

  /*
    Program to read a stream of characters from a file whose name is supplied as a
    command line argument, display it on the screen and write it onto a new file
  */

  #include<stdio.h>
  main(int argc, char *argv[])
  {
	FILE *fp1, *fp2;
	char chr;

	/* Open the existing file supplied as a command line parameter, in read mode */
	if((fp1 = fopen(argv[1], "r")) == 0)
		printf("\nERROR - Cannot open the indicated file.\n");

	/*
	  Read and display each character from the file and then write it onto
	  another file supplied as the second command line parameter
	*/
	else
	{
		fp2 = fopen(argv[2], "w");
		while(c != '\n')
		{
			putchar(chr = fgetc(fp1));
			putc(chr, fp2);
		}
	}

	/* close the data files */
	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