Как удалить ошибку сегментации при замене текста в файле?
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX 15 #define MAX_LEN 256 #define MAX_SIZE 32 void hello(){ /* Declaring variables */ int option, i=0, one_by_one=0, dob[11], id[13], phone_num[12], amount[1000000], fixing[13]; char file_text[100], str[100], word[MAX_LEN], updated_name[50], jao[100], read[100], name[15] = "", address[30] = "", account[10] = ""; short int FLAG = 0; static const char * listing[] = {"Name", "Date of birth","ID card number","Phone number","Address","Account","Fixing year","Deposit amount"}; FILE * fr2 = fopen("file.txt","r"); FILE * fr3 = fopen("file.txt","r"); FILE * fw1 = fopen("new.txt","w"); int c = fgetc(fr2); while(c != EOF){ file_text[one_by_one] = c; if(file_text[one_by_one] == ' '){ file_text[one_by_one] = '\0'; printf("Here is your %s: %s\n",listing[i],file_text); one_by_one = 0; i++; } else one_by_one++; c = fgetc(fr2); } fclose(fr2); /* Choice of different options to change */ if (fr3 == NULL || fw1 == NULL) { perror("Unable to read text file."); exit(0); } for (option = 1; option <= sizeof(listing)/sizeof(char *); ++option) printf("%d. Your %s\n", option, listing[option-1]); /* Here the choice is Old Word */ fputs("Select your choice to update: ", stdout); if ((scanf("%d", &option) != 1) || (option < 1) || (option > sizeof(listing)/sizeof(char *))) { puts("invalid choice"); fclose(fr3); exit(0); } /* Here is the New Word to replace with */ printf("Enter new name to update: "); fgets(updated_name, sizeof updated_name, stdin); rewind(fr3); while (!feof(fr3)) { fscanf(fr3, "%s", read); if (strcmp(read, jao) == 0) { // for deleting the word strcpy(read, updated_name); } // In last loop it runs twice fprintf(fw1, "%s ", read); } fclose(fw1); fclose(fr2); fclose(fr3); } int main(){ hello(); }
Что я уже пробовал:
Here is a huge code. You can copy and paste also but it is giving me segmentation error. How to avoide this? Because whenever I try to replace the text and update it in another file, it gives me segmentation error. As I am new here. So I am pretty confused how to solve this issue and replace the word properly.