PROGRAM MUST BE IN C. Decl of Indep.txt file has been included at the bottom of
ID: 3823325 • Letter: P
Question
PROGRAM MUST BE IN C.
Decl of Indep.txt file has been included at the bottom of the post. This is the text file where the substrings will be searched from.
Please do not use fscan() for the program.
An overview for the program:
1)Ask the user for the name of the input file.
a) If the file is not present or cannot be opened allow for an error message to be displayed.
2)Ask the user for a substring to search for.
3)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.
Decl of Indep.txt
------------------------------------
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 do, let's write a C program that locates the occurrences of a specific substring within a file and then display 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 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.txt"). Overview Here's a high level overview of what your program should do l) Ask the user for the name of the input file. 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) Ask the user for the substring to search for. 3) 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 l 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.Explanation / Answer
#include<stdio.h>
int main()
{
int c,i;
FILE *fp;
int filenamecorrect;
unsigned int max_len = 50;
unsigned int current_size = 0;
char *filename;
char *substring;
char *line;
filename = malloc(max_len);
substring = malloc(max_len);
line = malloc(max_len);
current_size = max_len;
filenamecorrect = 0;
while (filenamecorrect != 1)
{
printf(" Enter filename:");
if(filename != NULL)
{
i =0;
while (( c = getchar() ) != ' ')
{
filename[i++]=(char)c;
if(i == current_size)
{
current_size = i+max_len;
filename = realloc(filename, current_size);
}
}
filename[i] = '';
}
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error opening file, please enter correct filename ");
}
else
{
filenamecorrect = 1;
}
}
current_size = max_len;
printf(" Enter Substring:");
if(substring != NULL)
{
i =0;
while (( c = getchar() ) != ' ')
{
substring[i++]=(char)c;
if(i == current_size)
{
current_size = i+max_len;
substring = realloc(substring, current_size);
}
}
substring[i] = '';
}
char ch = getc(fp);
int count = 0;
int match = 0;
while ((ch != EOF)) {
current_size = max_len;
while ((ch != ' ') && (ch != EOF)) {
if (count == current_size) {
current_size = count + max_len;
line = realloc(line, current_size);
if (line == NULL) {
printf("Error reallocating space for line buffer.");
exit(1);
}
}
lineBuffer[count] = ch;
count++;
ch = getc(file);
}
line[count] = '';
if strstr(line,substring) != NULL) {
match++;
printf("%s %d %s","Location ", match, "String: ");
printf("%c %s %c ", '"', line, '"');
}
}
printf("%s %d %s %s %s %s ", "There were ", match, "occurences of the string ", substring, "in the file ", filename);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.