Hello I am working on a C program that reads a text file and search it for a str
ID: 3665441 • Letter: H
Question
Hello I am working on a C program that reads a text file and search it for a string! I am getting one error that i cannot figure out! Please help!! "Implicit declaration of function 'stringCompare' is invalid in C99"
Please help me work out this bug! My program so far:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fr,*fr1,*fr3; //declare pointer
char filename[256];
char search[256];
char line[256];
int count=0;
char replace[256];
printf("Enter FileName: "); // ask for filename
scanf("%s",filename);
fr3 = fopen (filename, "r");
if(ferror(fr3)) //check for any error assosicated to file
{
fprintf(stderr, "Error in file"); // print error in stderr
printf("Error");
}
printf("Input file data is");
while(!feof(fr3)) // checking end of file condition
{ //printf("inside");
fgets(line,256,fr3) ;
printf("%s",line);
}
printf(" Enter String to search: "); //ask for string to search
scanf("%s",search);
printf("Enter string with whom you want to replace: "); //ask for string with whom you want to replace one time
scanf("%s",replace);
fr = fopen (filename, "r"); // open first file in read mode
fr1 = fopen ("output.txt", "w"); //open second file in write mode
if(ferror(fr)) //check for any error assosicated to file
{
fprintf(stderr, "Error in file"); // print error in stderr
printf("Error");
}
while(!feof(fr)) // checking end of file condition
{ //printf("inside");
fgets(line,256,fr) ;
int r =stringCompare(line,search); // comparing every string and search string
// printf("%d",r);
if(r==1 && count==0)
{
fwrite(replace , 1 , sizeof(replace) , fr1 ); // writing string to file.
printf("%s ",replace);
count++;
}
else{
printf("%s",line);
fwrite(line , 1 , sizeof(line) , fr1 );
}}
printf(" ");
fflush(fr1); // it will flush any unwanted string in stream buffer
fflush(fr);
fflush(fr3);
fclose(fr); //closing file after processing
fclose(fr1);
fclose(fr3);
system("PAUSE");
return 0;
}
// Compare method which is comparing charaacter by character
int stringCompare(char str1[],char str2[]){
int i=0,flag=0;
while(str1[i]!='' && str2[i]!=''){
if(str1[i]!=str2[i]){
flag=1;
break;
}
i++;
}
if (flag==0 && (str1[i]=='' || str2[i]==''))
return 1;
else
return 0;
}
Explanation / Answer
Program:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int stringCompare(char str1[],char str2[]){
int i=0,flag=0;
while(str1[i]!='' && str2[i]!=''){
if(str1[i]!=str2[i]){
flag=1;
break;
}
i++;
}
if (flag==0 && (str1[i]=='' || str2[i]==''))
return 1;
else
return 0;
}
int main(int argc, char *argv[])
{
FILE *fr,*fr1,*fr3; //declare pointer
char filename[256];
char search[256];
char line[256];
int count=0;
char replace[256];
printf("Enter FileName: "); // ask for filename
scanf("%s",filename);
fr3 = fopen (filename, "r");
if(ferror(fr3)) //check for any error assosicated to file
{
fprintf(stderr, "Error in file"); // print error in stderr
printf("Error");
}
printf("Input file data is");
while(!feof(fr3)) // checking end of file condition
{ //printf("inside");
fgets(line,256,fr3) ;
printf("%s",line);
}
printf(" Enter String to search: "); //ask for string to search
scanf("%s",search);
printf("Enter string with whom you want to replace: "); //ask for string with whom you want to replace one time
scanf("%s",replace);
fr = fopen (filename, "r"); // open first file in read mode
fr1 = fopen ("output.txt", "w"); //open second file in write mode
if(ferror(fr)) //check for any error assosicated to file
{
fprintf(stderr, "Error in file"); // print error in stderr
printf("Error");
}
while(!feof(fr)) // checking end of file condition
{ //printf("inside");
fgets(line,256,fr) ;
int r =stringCompare(line,search); // comparing every string and search string
// printf("%d",r);
if(r==1 && count==0)
{
fwrite(replace , 1 , sizeof(replace) , fr1 ); // writing string to file.
printf("%s ",replace);
count++;
}
else{
printf("%s",line);
fwrite(line , 1 , sizeof(line) , fr1 );
}}
printf(" ");
fflush(fr1); // it will flush any unwanted string in stream buffer
fflush(fr);
fflush(fr3);
fclose(fr); //closing file after processing
fclose(fr1);
fclose(fr3);
system("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.