Click here to Skip to main content
14,733,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to replace the first occurence of a word in a file but the other files from the argv are not replaced. Here is my function:
void replace_first(char* oldWord,char* newWord)
{
    printf("f\n");
    FILE * fPtr;
    FILE * fTemp;
    FILE* f;
    char path[1000];
    char file[BUFFER_SIZE];
    char buff[BUFFER_SIZE];
    char buff_2[BUFFER_SIZE];
    printf("%d\n",num_files);
 //   char oldWord[100], newWord[100];
    f= fopen("files_to_be_scanned.txt", "r");
    while (!feof(f))
    {
    	fgets(file,sizeof(file),f);
       // file[strlen(file) - 1] = '\0';
    	printf("\nfile is %s\n",file);
        strcpy(path,file);
        printf("path is %s\n",path);
        path[strlen(path) - 1] = '\0';
        fPtr  = fopen(path, "r");
        if(fPtr == NULL)
        {
            perror("error in main files");
        }
        fTemp = fopen("replace_4.txt", "w");
        if(fTemp == NULL)
        {
            perror("error in replace file");
        }


        /* fopen() return NULL if unable to open file in given mode. */
        if (fPtr == NULL || fTemp == NULL)
        {
            /* Unable to open file hence exit */
            printf("\nUnable to open file.\n");
            printf("Please check whether file exists and you have read/write privilege.\n");
            exit(EXIT_SUCCESS);
        }



     while ((fgets(buff, BUFFER_SIZE, fPtr)) != NULL)
     {

        printf("beforeeeee %s\n",buff);
        char *pos,temp[BUFFER_SIZE];
        int index = 0;
        int owlen;
        owlen = strlen(oldWord);
        while ((pos = strstr(buff, oldWord)) != NULL)
        {
            first_flag=1;
            // Bakup current line
            strcpy(temp, buff);

            // Index of current found word
            index = pos - buff;

            // Terminate str after word found index
            buff[index] = '\0';

            // Concatenate str with new word
            strcat(buff, newWord);

            // Concatenate str with remaining words after
            // oldword found index.
            strcat(buff, temp + index + owlen);
            printf("TTTTTTTTTTTT %s\n",buff);
            fputs(buff, fTemp);
        }

        if(first_flag==1)
        {

            first_flag=0;
            break;
        }
       // printf("Afteeeeeeeeeeer %s\n",buff);

     }
       /* Close all files to release resource */
        fclose(fPtr);
        fclose(fTemp);


        /* Delete original source file */
       int del = remove(path);
       if (!del)
       {
            printf("The file is Deleted successfully\n");
       }
       else
       {
            printf("the file is not Deleted\n");
            perror("error in deleting file ");
       }


        /* Rename temp file as original file */
        rename("replace.txt", path);

        printf("\nSuccessfully replaced first occurrence of '%s' with '%s'.\n", oldWord, newWord);

    }

}

and the command line I execute with is:

main replace -f  I hoda  -- file1.txt file2.txt


What I have tried:

I have tried to not put break in
if(first_flag==1)
        {

            first_flag=0;
            break;
        }

but I do not find another way.
Posted
Updated 6-Feb-20 23:15pm
v2

When you posted this yesterday: Can not replace word in file using C language[^]
I told you that you need to use the debugger to work out what your code is doing, and from that you should be able to work out what you need to change: that doesn't change because it's Friday instead of Thursday!

Use the debugger: follow your code through, and find out exactly what path it is following, and compare that to the path you think it should be following. Look at the data it is processing to work out why there is a difference.
Debugging is a skill: you need to use it in order to improve it - and you want to get it up to a good level on jobs like this before you start meeting big, complicated projects.
   
Comments
Maciej Los 7-Feb-20 5:03am
   
Oh, yeah!
You read a list of filenames from files_to_be_scanned.txt, and for each name in that list you copy it to replace_4.txt, with any possible changes. So you never even look at the filenames on the command line.

You need to simplify your code considerably. Just take the input and output filenames from the command line and get the code working to process those. Use some fixed known text content and control words so you can see where any problems occur. You can add more enhancements once you have the basic process working.
   

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