PROGRAM MUST BE IN C. This is a program that is used to open the text file \"Dec
ID: 3824990 • Letter: P
Question
PROGRAM MUST BE IN C.
This is a program that is used to open the text file "Decl of Indep.txt" and search for substrings in the file. It is supposed to say how many times that substring is present and where it is located.
MY PROBLEM:
The program runs and works and can locate the Decl of Indep.txt file. However, whenever I put in the substring it crashes the console and stops the program from running. I need this to be fixed. Please do not change the purpose of the program but help me resolve the crashing problem so that it finds the substrings in the text file and shows how many times it has appeared in the file and where it is located.
It might be crashing because of the newline before fopen().
#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;
char *lineBuffer;
char *file;
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, "occurrences of the string ", substring, "in the file ", filename);
return 0;
}
Decl of Indep.txt
Explanation / Answer
your 2nd while loop which reads line till new line has errors, i modified the code check now
you are using linebuffer, and line instead of one.
#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;
char *lineBuffer;
char *file;
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)) {
//printf(" ch = -%c-",ch);
current_size = max_len;
count = 0;
line = malloc(max_len);
do {
// printf(" count = %d",count);
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;
line[count] = ch;
count++;
// printf("%c",ch);
ch = getc(fp);
} while(ch != ' ' && ch != EOF);
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, "occurrences of the string ", substring, "in the file ", filename);
return 0;
}
-----------output---------------
He has erected a multitude of New Offices, and sent hither swarms of "
ocation 46 String: "
Officers to harass our People, and eat out their substance. "
ocation 47 String: "
He has kept among us, in times of peace, Standing Armies without the "
ocation 48 String: "
He has affected to render the Military independent of and superior "
ocation 49 String: "
to the Civil Power. "
ocation 50 String: "
He has combined with others to subject us to a jurisdiction foreign "
ocation 51 String: "
Assent to their Acts of pretended legislation: "
ocation 52 String: "
For protecting them, by a mock Trial, from Punishment for any "
ocation 53 String: "
Murders which they should commit on the Inhabitants of these States: "
ocation 54 String: "
For cutting off our Trade with all parts of the world: "
ocation 55 String: "
For depriving us, in many cases, of the benefits of Trial by Jury: "
ocation 56 String: "
For abolishing the free System of English Laws in a neighbouring "
ocation 57 String: "
Province, establishing therein an Arbitrary government, and "
ocation 58 String: "
fit instrument for introducing the same absolute rule into these "
ocation 59 String: "
altering fundamentally the Forms of our Governments: "
ocation 60 String: "
For suspending our own Legislatures, and declaring themselves "
ocation 61 String: "
destroyed the lives of our people. "
ocation 62 String: "
to compleat the works of death, desolation and tyranny, already "
ocation 63 String: "
the most barbarous ages, and totally unworthy of the Head of a "
ocation 64 String: "
He has constrained our fellow Citizens taken Captive on the high "
ocation 65 String: "
Seas to bear Arms against their Country, to become the executioners "
ocation 66 String: "
of their friends and Brethren, or to fall themselves by their Hands. "
ocation 67 String: "
endeavoured to bring on the inhabitants of our frontiers, the "
ocation 68 String: "
n every stage of these Oppressions We have Petitioned for Redress in the "
ocation 69 String: "
hich may define a Tyrant, is unfit to be the ruler of a free People. "
ocation 70 String: "
arned them from time to time of attempts by their legislature to extend "
ocation 71 String: "
n unwarrantable jurisdiction over us. We have reminded them of the "
ocation 72 String: "
heir native justice and magnanimity, and we have conjured them by the "
ocation 73 String: "
ies of our common kindred to disavow these usurpations, which would "
ocation 74 String: "
een deaf to the voice of justice and of consanguinity. We must, "
ocation 75 String: "
herefore, acquiesce in the necessity, which denounces our Separation, and "
ocation 76 String: "
old them, as we hold the rest of mankind, Enemies in War, in Peace "
ocation 77 String: "
e, therefore, the Representatives of the United States of America, in "
ocation 78 String: "
eneral Congress, Assembled, appealing to the Supreme Judge of the world "
ocation 79 String: "
or the rectitude of our intentions, do, in the Name, and by the Authority "
ocation 80 String: "
f the good People of these Colonies, solemnly publish and declare, That "
ocation 81 String: "
hese United Colonies are, and of Right ought to be Free and Independent "
ocation 82 String: "
tates; that they are Absolved from all Allegiance to the British Crown, "
ocation 83 String: "
nd that all political connection between them and the State of Great "
ocation 84 String: "
ndependent States, they have full Power to levy War, conclude Peace, "
ocation 85 String: "
ontract Alliances, establish Commerce, and to do all other Acts and "
ocation 86 String: "
hings which Independent States may of right do. And for the support of "
ocation 87 String: "
his Declaration, with a firm reliance on the Protection of Divine "
ocation 88 String: "
rovidence, we mutually pledge to each other our Lives, our Fortunes and "
ocation 89 String: "
George Wythe "
ocation 90 String: "
John Witherspoon "
ocation 91 String: "
Matthew Thornton "
here were 91 occurrences of the string the in the file Decl of Indep.txt
:UsersSatish-Ravi>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.