**PROGRAM MUST BE COMPLETED IN C PROGRAMMING LANGUAGE** HERE IS THE PROGRAM FILE
ID: 3715988 • Letter: #
Question
**PROGRAM MUST BE COMPLETED IN C PROGRAMMING LANGUAGE**
HERE IS THE PROGRAM FILE DATA:
Program 2 - Detecting Substrings Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. I'm sure we've all done this many times in working with Word, Notepad, or other editors Since we don't have a GUI or other means of displaying the contents of a file all at once, let's modify the problem slightly. Rather than locating a specific substring within a file and then highlighting the results, as most modern programs would do, let's write a C program that locates the occurrences of a specific substring within a file and then displays the occurrence number as well as a portion of the text around the found substring. It should also count the number of occurrences and indicate that number in a brief report at the end Although it should work on any input file and any substring, we'll provide a copy of an input file to use for testing (i.e., "Decl of Indep.tx?') Overview Here's a high level overview of what your program should do 1) Ask the user for the name of the input file a)If the file is not present or can't be opened, display an error message and ask the user to enter another file name. 2) 3) Ask the user for the substring to search for. Calculate and display the found occurrences of the substring in the file. For each found occurrence, your program should display (a) the location number starting at 1 and going up to the total number of locations found, and (b) the portion of the string containing the found substring. This portion should consist of the substring and 8 characters before and 8 characters after the found substring. (Note that if the substring is within 8 characters of the end of a line this won't be possible.) Sample Runs Here is a sample run looking for the string "people" in the file "Decl of Indep.txt" Looking for the substring"people" in file "Decl of Indep.txt" Location 1:String:"for one people to" Location 2: String: "icts of people, unless" Location 3: String: "s those people would r" Location 4: String: of the people." Location 5: String:" of our people."Explanation / Answer
SourceCode:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#define fopen_s(fp,fmt,mode)
#define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename),(mode)))==NULL
int File_to_Search(char*,char*);
int File_to_Search(char *s, char *str)//Error expected ';',',',or')'
{
FILE *fp;
int line_num = 1;
int find_res_string = 0;
char temp[512];
if((fopen_s(&fp, s, "r")) != NULL) {
return(-1);
}
while(fgets(temp, 512, fp) != NULL) {
if((strstr(temp, str)) != NULL) {
printf("line : %d ", line_num);
printf("string: %s ", temp);
find_res_string++;
}
line_num++;
}
if(find_res_string == 0) {
printf(" Sorry, couldn't find a match. ");
}
//Close the file if still open.
if(fp) {
fclose(fp);
}
return(0);
}
int main(int argc, char *argv[]) {
int res_string, errno;
system("cls");
res_string = File_to_Search("Index.txt", "for one people to");
if(res_string == -1) {
perror("Error");
printf("Error number = %d ", errno);
exit(1);
}
return(0);
}
Note: save text file as Index.txt and it should be in the same root folder where c codefile is there.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.