menu.txt steak is good. steak is great. I could eat steak every day if I wouldn\
ID: 3735784 • Letter: M
Question
menu.txt
steak is good. steak is great. I could eat steak every day if I wouldn't die from gout...
expected_output.txt
chicken is good. chicken is great. I could eat chicken every day if I wouldn't die from gout...
Q3) Use a state machine to copy a file but replace the word "steak with the word "chicken. The input file is named menu.txt, and the output file is to be named output.txt. In other words your oode will open and read (character by character) from the input file menu.txt, and write (character by character) into a new file to be named output.txt. However, whenever you read the word "steak you should write instead "chicken into the output.txt, otherwise you write the exact same character that is read from the input menu.txt State Machine: A state machine switches tasks based on events. They are typically used for simple controllers or sequence detectors. Think of an utonomous car. If it veers too far to either side, the vehice will leave the road, and cause harm to it's passengers. Suppose the car had two states, 'just eft and adjust right'. The car may switch states to adjust the turning angle, thus keeping it's passengers safe and on the road. In this assignment we would like to detect the word "steak in the menu file. The state is switched based on if the current character is correct and is in the right order to form the word "steak. If it is correct, move to the next character in the word. If it is wrong. write all the characters to the output fie and go back to the initial state. This means that you read each character as it comes in and check if they are in the right order to form the word "steak". If the complete word "steak is found, then write the word "chickenin the output and go back to the initial state. You must use a switch statement. Consider the followg pseudooodeExplanation / Answer
#include <stdio.h>
//Function to replace 'steak' from input file with 'chicken' in output file.
void transformFile(FILE * fptr_R, FILE * fptr_W, int state);
int main(void) {
//File pointers for reading and writing in files.
FILE * fptr_R;
FILE * fptr_W;
//Opening input file
if((fptr_R = fopen("menu.txt","r")) == NULL){
printf("Error! opening input file");
return 1;
}
//Opening output file
if((fptr_W = fopen("output.txt","w")) == NULL){
printf("Error! opening output file");
return 1;
}
transformFile(fptr_R, fptr_W, 1);
fclose(fptr_R);
fclose(fptr_W);
//Opening result file to show the output on terminal
if((fptr_R = fopen("output.txt","r")) == NULL){
printf("Error! opening input file");
return 1;
}
char c;
while ((c = (char)fgetc(fptr_R)) != EOF){
printf("%c",c);
}
fclose(fptr_R);
return 0;
}
void transformFile(FILE * fptr_R, FILE * fptr_W, int state) {
//Read a char
char c = (char)fgetc(fptr_R);
//Check if the end of file is reached.
if(c == EOF)
return;
//State Machine: Taking action based on 'state' value
switch (state) {
case 1 : if (c=='s') {
//'s' found, looking for t now
state = 2;
}
else {
//not 'steak', writing the stacked characters.
fputc(c, fptr_W);
}
break;
case 2 : if (c=='t') {
//'st' found, looking for e now
state = 3;
}
else {
//not 'steak', writing the stacked characters.
fputc('s', fptr_W);
fputc(c, fptr_W);
state = 1;
}
break;
case 3 : if (c=='e') {
//'ste' found, looking for a now
state = 4;
}
else {
//not 'steak', writing the stacked characters.
fputc('s', fptr_W);
fputc('t', fptr_W);
fputc(c, fptr_W);
state = 1;
}
break;
case 4 : if (c=='a') {
//'stea' found, looking for k now
state = 5;
}
else {
//not 'steak', writing the stacked characters.
fputc('s', fptr_W);
fputc('t', fptr_W);
fputc('e', fptr_W);
fputc(c, fptr_W);
state = 1;
}
break;
case 5 : if (c=='k') {
//'steak' found, replacing it with chicken in output file.
fputc('c', fptr_W);
fputc('h', fptr_W);
fputc('i', fptr_W);
fputc('c', fptr_W);
fputc('k', fptr_W);
fputc('e', fptr_W);
fputc('n', fptr_W);
state = 1;
}
else {
//not 'steak', writing the stacked characters.
fputc('s', fptr_W);
fputc('t', fptr_W);
fputc('e', fptr_W);
fputc('a', fptr_W);
fputc(c, fptr_W);
state = 1;
}
break;
}
return transformFile(fptr_R, fptr_W, state);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.