C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

SOLVED QUESTION FILE MANAGEMENT 

1. WAP to create a file and write some text to it, writing one character at a time using the fputc() function. The program should write until the user hits the enter key. Read filename from user.


#include<stdio.h>
#include<stdlib.h>
int main()
{
	FILE *fptr;
	char c;
	fptr=fopen("abc2.txt","w");
	if(fptr==NULL)
	{
		printf("file cannot be open");
		exit(0);
	}
	else
	{
		printf("file created successfully");
			printf("enter text till enter key is hit\n");
			do
			{
				
				c = getchar();
				fputc(c,fptr);
			}while(c!='\n');
	}

	fclose(fptr);
}

2. WAP that opens a file and copies all its content to another file . Your program should read the name of the source and destination file from the user.

#include<stdio.h>
int main()
{
	FILE *sptr,*dptr;
	char c;
	sptr=fopen("abc.txt","r");
	if(sptr==NULL)
	{
		printf("file cannot be open");
	}
	dptr=fopen("xyz.txt","w");
	if(dptr==NULL)
	{
		printf("file cannot be open");
	}

	while((c=fgetc(sptr))!=EOF)
	{
		fputc(c,dptr);
	}
	fclose(sptr);
	fclose(dptr);
}

3. WAP  to create a file named student.txt in D:\ drive and write the name,roll,address and marks of a student to the file.

#include<stdio.h>


int main()
{
FILE *fptr;
char name[200];
int roll;
float marks;
fptr = fopen("abc.txt", "w");
if(fptr==NULL)
{
printf("file cannot be open\n");
}
printf("enter name,roll, marks ");
scanf("%s%d%f", name, &roll, &marks);
fprintf(fptr, "%s %d %f", name, roll, marks);
fclose(fptr);
}


4. WAP to create a data file and write the natural numbers from 1 to 20 to the file then read the numbers from the file to display the twice of stored numbers.

#include<stdio.h>


int main()
{
FILE *fptr;
int i,num;
fptr = fopen("abc.txt", "w+");
if(fptr==NULL)
{
printf("file cannot be open\n");
}
printf("writing natural number to file");
for(i=1;i<21;i++)
{
fprintf(fptr, "%d\n",i);
}
rewind(fptr);
printf("printing twice the number ");
for (i = 1; i < 21;i++)
{
fscanf(fptr, "%d", &num);
num = 2 * num;
printf("%d\n", num);
}
fclose(fptr);
}

5. Create a structure named Employee that has name,address and salary as member . Assume appropriate types and size of members. Use this structure to read and display records of 3 Employees. Write an array of structure to a file and then read its content to display to the screen.[structure, fread() ,fwrite()]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	struct Employee
	{
		char name[20];
		int age;
		float salary;
	}e[3];
	int i;
	FILE *fptr;
	fptr=fopen("employee.txt","w+");
	if(fptr==NULL)
	{
		printf("file cannot be open");
		exit(1);
	}
	for(i=0;i<3;i++)
	{
		printf("name\t age\t salary");
		scanf("%s%d%f",e[i].name,&e[i].age,&e[i].salary);
	}
	printf("writing information to file\n");
	fwrite(&e,sizeof(e),3,fptr);
	fflush(stdin);
	rewind(fptr);
	printf("reading the content from file\n");
	fread(&e,sizeof(e),3,fptr);
	printf("students information is");
	for(i=0;i<3;i++)
	{
		printf("%s\t%d\t%f",e[i].name,e[i].age,e[i].salary);
	}
	fclose(fptr);
}

6. WAP that create a file called “number.txt”, write an array of 10 digit integer number  into the file , again read the array stored in it and display to the screen.[array]


#include<stdio.h>
int main()
{
	FILE *fptr;
	int num[10],i;
	fptr=fopen("number.txt","w+");
	if(fptr==NULL)
	{
		printf("file cannot be open");
	}
	else
	{
		printf("file created successfully\n");
		printf("writing array to file\n");
		for(i=0;i<10;i++)
		{
			scanf("%d",&num[i]);
			fprintf(fptr,"%d",num[i]);
		}
		rewind(fptr);
		printf("reading an array from file\n");
		fread(&num,sizeof(num),1,fptr);
		for(i=0;i<10;i++)
		{
			printf("%d\t",num[i]);
		}
	//rewind(fptr);
	
}
	fclose(fptr);
}


7. Create a structure named Employee that has name,address and salary as member . Assume appropriate types and size of members. Use this structure to read and display records of 3 Employees. Write this structure to a file .[using fwrite]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	struct Employee
	{
		char name[20];
		int age;
		float salary;
	}e;
	int i;
	FILE *fptr;
	fptr=fopen("employee1.txt","w+");
	if(fptr==NULL)
	{
		printf("file cannot be open");
		exit(1);
	}

	
		printf("name\t age\t salary");
		scanf("%s",e.name);
		scanf("%d",&e.age);
		scanf("%f",&e.salary);
	
	printf("writing information to file\n");
	fwrite(&e,sizeof(e),1,fptr);
	
	fclose(fptr);
}


8. Create a structure named Employee that has name,address and salary as member . Assume appropriate types and size of members. Use this structure to read and display records of 3 Employees. Write this structure to a file .[using fprintf]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	struct Employee
	{
		char name[20];
		int age;
		float salary;
	}e;
	int i;
	FILE *fptr;
	fptr=fopen("employee1.txt","w+");
	if(fptr==NULL)
	{
		printf("file cannot be open");
		exit(1);
	}

	
		printf("name\t age\t salary");
		scanf("%s",e.name);
		scanf("%d",&e.age);
		scanf("%f",&e.salary);
	
	printf("writing information to file\n");
    fprintf(fptr,"%s%d%f",e.name,e.age,e.salary);
	
	fclose(fptr);
}