Your Ad Here

Thursday, July 5, 2007

File Handling - C Program

This is a C program on file Handling

/************** File Handling *************/

void main()
{
FILE *file1;
char c;
int choice;
char op;
clrscr();
do
{
printf("\t\tMenu\n\t1. Enter The Information\n\t2. Display The Information\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\n Information \n");
file1=fopen("Handlin.doc","w");


while((c=getchar())!=EOF)
{

putc(c,file1);
}
fclose(file1);
break;

case 2:
printf("Information\n");
printf("Result :");
file1=fopen("Malcolm .doc","r");
while((c=getc(file1))!=EOF)
printf("%c",c);
fclose(file1);
break;

default:
exit(1);
}
printf("Enter Y To Continue:");
flushall();
scanf("%c",&op);
}
while((op=='y')(op=='Y'));
getch();
}
/********************* OUTPUT **********************
Menu
1. Enter The Information
2. Display The Information
1

Information
Today Is Sunday
^Z
Enter Y To Continue:y
Menu
1. Enter The Information
2. Display The Information
2
Information
Result :
Today Is Sunday
Enter Y To Continue:n

*/

5 comments:

Unknown said...

can u plz explain this program to me....and it would be better if u could post a c++ version of this one.....

Anonymous said...

can u plz explain this program to me....and it would be better if u could post a c++ version of this one.....

Unknown said...

could u please explain this program to me....and it would be better for me if u could also post a c++ version of this program...

Mayur said...

There are a few problems in the program, firstly when you run the program and enter '1', the file will go infinite loop since you've specified !=EOF condition to a file where user will enter his info.
Secondly the program has a bug that it will not display the entered info since your case2 condition reads a file which is different from the file in case1.
After making these changes, I could run the program. :)

Anonymous said...

can u plz explain the program that accepts a file name form command line argument and stored conted offile in reverse order into another file

Your Ad Here