C programming- TEXT FILES Struct array shall have at least 4 fields, with at lea
ID: 3709832 • Letter: C
Question
C programming- TEXT FILES
Struct array shall have at least 4 fields, with at least one of these being a string and at least one being a number (int or double).
When the user runs the program, they shall be greeted with a menu of options like what is shown below (What would you like to do (A=Add record to list, W=Write all records to file, R=Read all records from file, E=Exit): .
This menu shall be repeated to the user (over and over) until the user finally selects "E" to exit (at which point the program shall stop).
If the user chooses "A", they shall be prompted to enter a struct (record), this record shall be added to the end of your struct list, and a report shall be printed that shows the record that was just added to the end of the list.
If the user chooses "W", the program shall write all the structs (one struct per line) to a text file.
If the user chooses "R", the program shall read all the struct data from a text file (one line holds one struct). This operation shall replace the current struct array. Then, the program shall print a report showing all the struct data (just read from the text file).
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//define the structure
struct Fruit{
char name[20];
char family[30];
int quantity;
double price;
};
struct Fruit fruitList[100];
int fruitListCount =0;
void showMenuList(){
printf( "Select one of the folllowing options " );
printf( "A=Add record to list " );
printf( "W=Write all records to filen" );
printf( "R=Read all records from file " );
printf( "E=Exit" );
printf( "Input Selection " );
}
int main(){
char choice[20];
char *item;
int i;
do{
showMenuList();
scanf(" %s", choice);
if(strcmp(choice,"A") == 0){
printf("Enter the fruitDetails: name family quantity price ");
scanf( "%s", fruitList[fruitListCount].name);
scanf( "%s", fruitList[fruitListCount].family);
scanf( "%d", &fruitList[fruitListCount].quantity);
scanf( "%lf", &fruitList[fruitListCount].price);
fruitListCount++;
}
else if(strcmp(choice,"W") == 0){
printf("Chose to write ");
FILE *fptr;
fptr = fopen("fruitLIst.txt", "w");
if(fptr == NULL)
{
printf("Error cant wite to file fruitLIst.txt!");
exit(1);
}
int i;
for(i=0; i<fruitListCount; i++){
fprintf(fptr,"%s %s %d %lf ", fruitList[i].name, fruitList[i].family, fruitList[i].quantity, fruitList[i].price);
}
fclose(fptr);
}
else if(strcmp(choice,"R") == 0){
printf("Chose to read ");
FILE *fptr;
fptr = fopen("fruitLIst.txt", "r");
if(fptr == NULL)
{
printf("Error cant read from file fruitLIst.txt!");
exit(1);
}
fruitListCount=0; //erasing earlier list
char line[151];
while (fgets(line,150,fptr)) {
printf("%s",line);
item = strtok(line," ");
strcpy(fruitList[fruitListCount].name,item);
item = strtok(NULL," ");
strcpy(fruitList[fruitListCount].family,item);
item = strtok(NULL," ");
fruitList[fruitListCount].quantity = atoi(item);
item = strtok(NULL," ");
fruitList[fruitListCount].price = atof(item);
fruitListCount++;
}
fclose(fptr);
/* Loop through and report on data */
printf("FruitList Record ");
for (i=0; i<fruitListCount; i++) {
printf("%d %s %s %d %lf ",i+1, fruitList[i].name, fruitList[i].family, fruitList[i].quantity, fruitList[i].price);
}
}
else if(strcmp(choice,"E") == 0){
printf("Quiting the program ");
}
else{
printf("Invalid Choice ");
}
}while(strcmp(choice,"E")!=0);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.