Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void firstWord(char str[]){
        int i = 0;
        while(!isspace(str[i])){
          i++;
        }
        str[i]='\0';
    }
    
    void hello(){
    	int option;
    	char updated_name[50], read[100];
    	short int FLAG = 0;
    	
    	static const char * listing[] = {"Name", "Date of birth","ID card number","Phone number"};
    	
      	FILE * fr3 = fopen("file.txt","r");
      	FILE * fw1 = fopen("new.txt","w");
      	
    	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]); 

<big>SELECT OPTION TO UPDATE</big>
    
    	fputs("Select your choice to update: ", stdout);
    	scanf("%d", &option);
    
    	char string[100];
    	if (option == 1){
    		while(fgets(string, 100, fr3) != NULL){
    			firstWord(string);
    			printf("'%s' found. Now replace it with another name: ", string);
    			scanf("%s", &updated_name);
    
    			fclose(fr3);
    		 	exit(0);
    		}
    	}
    	fclose(fw1);
    }
    int main(){
      hello();
    }


What I have tried:

I have this code which I divided into two parts just for understanding purpose. The first option is working fine and showing me details to update.

I have a problem with the second part in which a code is taking name/word from the file correctly but I don't know how to replace it with the new name/word in another file? Could you help me with some code because I searched a lot? Thanks!

Here is file.txt
bilalkhan 20/20/1980 908732343
Posted
Updated 28-Jul-20 20:02pm
v2

1 solution

The way I deal with text replacement is I open a temporary file to be written to. I open the input file and continually read from it into an internal buffer and replace data as necessary and then write the result to the temporary file. When the input has been completely read then close both files. The next steps are to copy the temporary file to the input file and then delete the temporary file. You can use fgets to read the text in as you are already. You just need to open the temporary file and write to it.

Initially, keep the temporary file around and don't copy or delete it so you can verify that you are doing things correctly and you do leave the input file as it is. When you are writing the file correctly then you can add logic to copy the temporary file over and then delete it but wait to do this until everything else works.
 
Share this answer
 
Comments
CPallini 29-Jul-20 2:03am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900