Question:
Write a C Program to display the following menu?
sumit
2010-08-23 04:29:53 UTC
Menu
1. Display
2. Copy
3. Append
4. Exit
Accept the choice (1-4) from the user, and perform the 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
Five answers:
MichaelInScarborough
2010-08-24 19:27:15 UTC
Maybe this one is helpful.



#include

#include

#include

#include



const char szMenu[] = "\nMenu"

"\n1. Display"

"\n2. Copy"

"\n3. Append"

"\n4. Exit > ";



char *get_filename(const char *szPrompt)

{

char szBuffer[1024];

int iContinue = 1;

char *ptr = NULL;

do {

iContinue = 0;

printf(szPrompt);

fgets(szBuffer, sizeof(szBuffer), stdin);

ptr = szBuffer;

szBuffer[strlen(szBuffer) - 1] = 0;

while(isspace(*ptr))

++ptr;;

if (!*ptr) {

iContinue = 1;

}

} while(iContinue);

return strdup(szBuffer);

}



FILE *get_file(const char *szPrompt, const char *szOpenMode)

{

char *fn = get_filename(szPrompt);

FILE *f = fopen(fn, szOpenMode);

free(fn);

return f;

}



void display_file()

{

FILE *f = get_file("Enter File to display: > ", "rt");

char buffer[1024];



if (f) {

while(fgets(buffer, sizeof(buffer), f)) {

fprintf(stdout, buffer);

}

fclose(f);

}

}



void process_file(const char *szOpenMode, long lOffset)

{

char buffer[1024];

FILE *f = get_file("Enter Source File: > ", "rt"),

*o = get_file("Enter Destination: > ", szOpenMode);

if (f && o) {

fseek(o, 0, lOffset);

while(fgets(buffer, sizeof(buffer), f)) {

fprintf(o, buffer);

}

}

if (f) {

fclose(f);

}

if (o) {

fclose(o);

}

}



void copy_file()

{

process_file("wt", SEEK_SET);

}



void append_file()

{

process_file("a", SEEK_END);

}



int get_choice(int iFirst, int iLast)

{

int iInput = 0;

do {

iInput = 0;

printf(szMenu);

scanf("%d", &iInput);

while(getchar() != '\n');

} while(iInput < iFirst || iInput > iLast);

return iInput;

}



int process()

{

switch(get_choice(1, 4)) {

case 1:

display_file();

break;

case 2:

copy_file();

break;

case 3:

append_file();

break;

case 4:

return 0;

break;

}

return 1;

}



int main()

{

do {

}while(process());

return 0;

}
Shadow Wolf
2010-08-24 05:40:36 UTC
Got to love homework problems.



Generally in C, you use printf() to display text. To get text usually you want scanf(). It seems one answerer is confusing C with C++. You'll want to loop back to the menu after performing the selected task if the menu option selected wasn't an exit.



Then, depending on how you are supposed to complete the assignment, you either want to write 3 functions to handle the operations or simply use the DOS commands to do the work for you using system(). Either way is pretty trivial since it is basic file I/O if you write the functions or building the proper DOS command line text for a system() command.



Your completed program probably won't exceed 50 lines no matter which way you elect to do the problem. The file I/O stuff can be written in fairly compact loops.



When you get the program written and you are still having problems, you can post the source code and someone can help you fix any specific problems. Your current "question" is simply a homework problem and it is actually in violation of Yahoo Answers rules since it isn't a question. Changing the punctuation of a request does not make it a question.



Shadow Wolf
Arkane Steelblade
2010-08-23 14:40:46 UTC
You have the answer in front of you, just put that into a function that returns the selection number



int function choice()

{

cout<<" MENU "<
cout<<"1) Display"<
...

..

cin>>get_choice;



return get_choice;

}



I didn't fill the rest, that is up to you, but just to give you an idea on how to do it and get you started.
tbshmkr
2010-08-23 13:31:57 UTC
Show your code.

=

We will help you make it work right.
JoelKatz
2010-08-23 11:43:48 UTC
You forgot to ask a question. What's your question about that assignment?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...