Write a C program that will display text on any terminal. The program should fir
ID: 3825090 • Letter: W
Question
Write a C program that will display text on any terminal. The program should first prompt the user for the name of the device le for the terminal on which to display output, as well as the output to display. It should then perform the necessary I/O operations. This process should repeat until the user decides to exit the program. The image below should be what the output may look like depending on the user's machine.
Enter the terminal to display in: /dev/pts/1 Enter the text to display: Test1 Test1 Enter the terminal to display in: /dev/pts/2 Enter the text to display: Test2 Unable to open /dev/pts/2 for writing Enter the terninal to display in: eV Enter the text to display: Test Test Enter the terminal to display in exitExplanation / Answer
#include <stdio.h>
#include <string.h>
int main()
{
int result;
char fileName[100];
char exitFilename[4]="exit";
char searchString[200];
do
{
printf("Enter the terminal to display in");
scanf("%s",fileName);
printf("Enter the text to display");
scanf("%s",searchString);
result = SearchingString(fileName, searchString);
}
while(strcmp(fileName,exitFilename) != 0);
return 0;
}
int SearchingString(char *fname, char *str) {
int line_num = 1;
int find_result = 0;
char temp[512];
FILE *fp=fopen(fname,"r");
while(fgets(temp, 512, fp) != NULL) {
if((strstr(temp, str)) != NULL) {
printf("A match found on line: %d ", line_num);
printf(" %s ", temp);
find_result++;
}
line_num++;
}
if(find_result == 0) {
printf(" Sorry, couldn't find a match. ");
}
if(fp) {
fclose(fp);
}
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.