¤ 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


Web Service Offers

Third Level Dot In Registration
599 199 (66.78% off)
per Year

.IN Registration
749 199 (73.43% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)


Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting 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