133k views
1 vote
Given a text file containing the availability of food items, write a program that reads the information from the text file and outputs the available food items. The program should first read the name of the text file from the user. The program then should read the text file, line by line. If a food is available, the program should output the available food item in the following format: name (category) -- description

Assume the text file contains the category, name, description, and availability of at least one food item, separated by a tab character ('\t').
Hints: Use the find() function to find the index of a tab character in each row of the text file. Use the substr() function to extract the text separated by the tab characters.

GIVEN CODE IN C BELOW

#include
#include

int main(void) {
const int MAX_LINES = 25; // Maximum number of lines in the input text file
const int MAX_STRING_LENGTH = 100; // Maximum number of characters in each column of the input text file
const int MAX_LINE_LENGTH = 200; // Maximum number of characters in each line of the input text file

// Declare 4 string arrays to store the 4 columns from the input text file
char column1[MAX_LINES][MAX_STRING_LENGTH];
char column2[MAX_LINES][MAX_STRING_LENGTH];
char column3[MAX_LINES][MAX_STRING_LENGTH];
char column4[MAX_LINES][MAX_STRING_LENGTH];

/* Type your code here. */

return 0;
}

User Amos Yuen
by
8.6k points

1 Answer

1 vote

Final Answer:

C program prompts the user to input a text file's name containing food availability information, reads and processes the file, and outputs available food items in a specific format. It utilizes arrays and file handling functions in C.

Step-by-step explanation:

```c

#include <stdio.h>

#include <string.h>

int main(void) {

const int MAX_LINES = 25; // Maximum number of lines in the input text file

const int MAX_STRING_LENGTH = 100; // Maximum number of characters in each column of the input text file

const int MAX_LINE_LENGTH = 200; // Maximum number of characters in each line of the input text file

char column1[MAX_LINES][MAX_STRING_LENGTH];

char column2[MAX_LINES][MAX_STRING_LENGTH];

char column3[MAX_LINES][MAX_STRING_LENGTH];

char column4[MAX_LINES][MAX_STRING_LENGTH];

char filename[MAX_STRING_LENGTH];

printf("Enter the name of the text file: ");

scanf("%s", filename);

FILE *file = fopen(filename, "r");

if (file != NULL) {

// Read the text file, extract columns, and output available food items

while (fscanf(file, "%s\t%s\t%s\t%s", column1[i], column2[i], column3[i], column4[i]) != EOF) {

if (strcmp(column4[i], "available") == 0) {

printf("%s (%s) -- %s\\", column2[i], column1[i], column3[i]);

}

}

fclose(file);

} else {

printf("Error opening the file.\\");

}

return 0;

}

```

In this program, the user is prompted to enter the name of a text file containing information about food items. The program then reads the file line by line, extracting information from each line based on tab-separated values.

If a food item is available, it outputs the relevant information in the specified format. This program utilizes arrays to store the different columns from the input file and incorporates file handling functions in C for reading and processing the text file.

User Kenny Bones
by
8.0k points