Code:
#include <stdio.h>
#include <stdlib.h>
#define QUIT 5
char filename[80];
char i[20], in[20], inf[20], info[20], infor[20], inform[20], informa[20], informat[20], informati[20], informatio[20], information[20];
int get_menu( void );
FILE *fp;
main()
{
int choice = 0;
while (choice != QUIT)
{
choice = get_menu();
if (choice == 1)
{
// creates file
puts("Enter a name for the file:");
scanf("%s", &filename);
if ((fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "Error creating file %s", filename);
exit(1);
}
fclose(fp);
}
if (choice == 2)
{
//edits file
puts("Enter a file to edit:");
scanf("%s", &filename);
if ((fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
printf("Enter your information(no more than 11 words long): ");
scanf("%d %d %d %d %d %d %d %d %d %d %d", &i, &in, &inf, &info, &infor, &inform, &informa, &informat, &informati, &informatio, &information);
fprintf(fp, "%d %d %d %d %d %d %d %d %d %d %d", i, in, inf, info, infor, inform, informa, informat, informati, informatio, information);
fclose(fp);
}
if (choice == 3)
{
//reads a file
puts("Enter a file to read:");
scanf("%s", &filename);
if ((fp = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "No exsiting file.");
exit(1);
}
fscanf(fp, "%d %d %d %d %d %d %d %d %d %d %d", &i, &in, &inf, &info, &infor, &inform, &informa, &informat, &informati, &informatio, &information);
printf("%d %d %d %d %d %d %d %d %d %d %d", i, in, inf, info, infor, inform, informa, informat, informati, informatio, information);
fclose(fp);
}
if (choice == 4)
{
// deletes file
puts("Enter a filename to remove: ");
scanf("%s", &filename);
if ( remove(filename) == 0)
{
printf("Succesful deletion of %s", filename);
}
else
{
fprintf(stderr, "Error deleting file %s", filename);
exit(1);
}
}
}
return (0);
}
int get_menu( void )
{
int selection = 0;
/* display menu() to screen */
do
{
printf("\n");
printf("1 - Create file\n");
printf("2 - Edit file\n");
printf("3 - Read file\n");
printf("4 - Delete file\n");
printf("5 - Quit\n");
printf("\n");
printf("\nEnter what you would like to do: ");
scanf("%d", &selection);
}
while (selection < 1 || selection > 5);
return selection;
}