So this creates a file, stores some text and presents a menu. Currently only 1 option is to censor the file. I created read_string to open the file and take the text from there. So I want to censor the text from that file but it doesn't seem to call from read_string.
insert
Code:
char read_string(char *fname, char *sentence){
printf("Enter the filename:");
scanf("%s",fname);
FILE* ptr = fopen(fname, "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
while (!feof(ptr)) {
char sentence1 = fgetc(ptr);
printf("%c", sentence);
}
fclose(ptr);
}
void store_file(){
char str[1000];
FILE *fptr;
char fname[200];
char sentence[2000];
printf("Input file path:");
scanf("%s",fname);
fflush(stdin);
fptr=fopen(fname,"w");
if(fptr==NULL)
{
printf(" Error in opening file!");
exit(1);
}
printf(" Input a sentence for the file : ");
fgets(str, sizeof str, stdin);
fprintf(fptr,"%s",str);
fclose(fptr);
printf("\n The file %s created successfully...!!\n\n",fname);
fflush(stdin);
}
char *censor(char *sentence, char *word){
char *p;
for (p = sentence; (p = strstr(p, word)) != NULL; )
while (isalpha(*p))
*p++ = '*';
printf("%s\n", sentence);
}
void censor_main(){
char fname[500];
char sentence[1024];
read_string(fname,sentence);
fgets(sentence, sizeof sentence, stdin);
int len = strlen(sentence);
if (len && sentence[len - 1] == '\n') sentence[--len] = '\0';
printf("Enter a word to censor: ");
char word[128];
fgets(word, sizeof word, stdin);
len = strlen(word);
if (len && word[len - 1] == '\n') word[--len] = '\0';
censor(sentence,word);
}