Sunday, July 26, 2009

C Program Help?

Hi Can anyone help me wrting a C Program to display following menu





Menu





1. Display





2. Copy





3. Append





4. Exit





Accept the choice (1-4) from the user, and perform following tasks:





Choice 1: Accept a file name from the user and display the file on screen





Choice 2: Accept two file names, and copy first file to the second





Choice 3: Accept two file names, and append second file to the first file





Choice 4: Terminate the program


program should have appropriate validations

C Program Help?
This code works perfectly...





#include%26lt;stdio.h%26gt;








void display(void);





void copy_it();





void append_it(void);











int main()


{


int choice;








while(1)


{





printf("\nMenu :: \n 1.Display \n 2.copy \n 3. append \n 4. terminate");


scanf("%d",%26amp;choice);





switch(choice)


{


case 1:


display();


break;





case 2:


copy_it();


break;





case 3:


append_it();


break;





case 4:


return 0;





}


}





}





void display(void)


{


FILE *fp; //File pointer


char file_path[150];


char c;





printf("\nEnter the file name...");


scanf("%s",file_path);





fp = fopen(file_path,"rb");





if(fp == NULL)


{


printf("\nFile open failed");


return;


}





while(!feof(fp))


{


c=fgetc(fp);


if(feof(fp))


break;





printf("%c",c);


}


fclose(fp);


return;





}








void copy_it()


{


FILE *fp,*to;


char file_path[150],to_file_path[150];


char c;





printf("\nEnter the source file name");


scanf("%s",file_path);





printf("\n\nEnter the destination file path");


scanf("%s",to_file_path);





fp = fopen(file_path,"rb");


to = fopen(to_file_path,"wb+");





if(!fp || !to)


{


printf("\nFile open failed");


return;


}





while(!feof(fp))


{


c=fgetc(fp);


if(feof(fp))


break;





fputc(c,to);


}





fcloseall();


return;


}








void append_it(void)


{


FILE *fp,*to;


char file_path[150],to_file_path[150];


char c;





printf("\nEnter the source file name");


scanf("%s",file_path);





printf("\n\nEnter the destination file path");


scanf("%s",to_file_path);





fp = fopen(file_path,"rb");


to = fopen(to_file_path,"ab+");





if(!fp || !to)


{


printf("\nFile open failed");


return;


}





while(1)


{


c=fgetc(fp);


if(feof(fp))


break;





fputc(c,to);





}





fcloseall();


return;


}











You can improve this by using other functions like fread and fwrite
Reply:Go to planetsourcecode.com There u will find readymade program for anything





Visit:


http://www.expertsview.blogspot.com/


http://www.khaliali.blogspot.com/


http://www.cetricks.blogspot.com/
Reply:Send your mail id to me I'll send you the source code....





It's a very easy one
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


FILE *fsouce,fdest;


void main()


{


int choice;


printf("Menu\n1.Display\n2.Copy\n3.App...





printf("Select the choice(1-4)\n");


scanf("%d",%26amp;choice);


if(choice==1)


display();


if(choice==2)


copy();


if(choice==3)


append();


if(choice==4)


exit(1);


getch();


}


void display()


{


char filename[20];


char c;


printf("enter the filename\n");


scanf("%s",filename);


fsource=fopen(filename,"r")


if(fsource==NULL)


printf("file doesn't exist\n");


else


{


do{


c=getc(fsource);


putchar(c)


}


while(c!=EOF);


}


fclose(funny);


}

dried flowers

No comments:

Post a Comment