thekevshow
thekevshow

Reputation: 784

Replace String in Text C language

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ( int argc, char *argv[] )
{
    if ( argc != 4 ) /* argc should be 4 for correct execution */
    {
        /* Print argv[0] assuming it is the program name */
        printf( "usage: %s filename\n", argv[0] );
    }
    else 
    {
        // We assume argv[1] is a filename to open
        char* wordReplace = argv[1];
        char* replaceWord = argv[2]; 
        FILE *file = fopen( argv[3], "r+" );
        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            char string[100];
            int len = 0;int count = 0;int i = 0;int k = 0;
            while  ( (fscanf( file, "%s", string ) ) != EOF )
            {
                len = strlen(string);
                count++;
                char charray[len+1];
                if(count == 1)
                {
                    for (i = 0; i < len; i++)
                    {
                        charray[i] = replaceWord[i];
                        printf("%c\n", charray[i]);
                    }
                }
                //printf("%c\n", charray[0]);
                printf( "%s\n", string );
                if(strcmp(string, wordReplace) == 0)
                {
                    for(k = 0; k < strlen(replaceWord); k++)
                    {
                         fseek (file, (-(long)len), SEEK_CUR);
                         fputc(charray[k],file);
                         //replaceWord++;
                    }
                    //strcpy(string, replaceWord);
                    //fprintf(file,"%s",replaceWord);
                    //fputs(string, file);
                    //printf("\n%d\n", len);
                }       
            }
            fclose( file );
        }
    }
    printf("\n");
    return 0;
}

This code currently works in replacing the First word properly, but if there are multiple words that i want overwritten with the replace word or the word appears somewhere else in the text it will not properly change it, and it will change it to ram trash etc. I was curious if anyone could lead me to a reason why thank you.

Upvotes: 0

Views: 4521

Answers (1)

msam
msam

Reputation: 4287

Assuming the words are the same length (if not you have quite a few more issues): Let's say you have a 4 character word: fseek (file, (-(long)len), SEEK_CUR); will go back to position 0 (4-4), fputc(charray[k],file); will update to position 1, then you back 4 more which is an error but since you're not checking the return value from fseek you will not know this. At this point the algorithm is not working any more since your assumed file positions are all wrong

EDIT:

if(strcmp(string, wordReplace) == 0)
{
    fseek (file, (-(long)len), SEEK_CUR);
    for(k = 0; k < strlen(replaceWord); k++)
    {                         
        fputc(charray[k],file);
    }

} 
fflush(file); //you need to flush the file since you are switching from write to read

EDIT 2: reason for flush: from 4.5.9.2 ANSI C, similar paragraph in C99 7.19.5.3):

When a file is opened with update mode ('+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, output may not be directly followed by input without an intervening call to the fflush function or to a file positioning function ( fseek , fsetpos , or rewind ), and input may not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

Between the read and write you have the fseek already so that is not a problem

Upvotes: 1

Related Questions