D
D
d3coy_002020-12-10 21:55:52
C++ / C#
d3coy_00, 2020-12-10 21:55:52

Editing a file in C?

Hello, I read the text from the file into the char array of the strFile variable, I got a pointer to the beginning of the word that needs to be replaced in the array (+ the question is, how to find this word without specifying it completely?)

How can I replace the string in this buffer through the for and if loop inside if it is known that from the pointer +11 characters this is the word that needs to be changed for example to photo27.jpg and write the buffer back to the file?

int main(int argc, char* argv[])
{
    char* sPath_in = "log.txt";

    FILE *f = fopen(sPath_in, "rb"); 
    fseek(f, 0, SEEK_END);
    long FileLength = ftell(f);
    fseek(f, 0, SEEK_SET);
    
    
    char *strFile = (char *)malloc(FileLength + 1);
    fread(strFile, FileLength, 1, f);
    char * strfound = strstr(strFile, "photo26.jpg");

    printf("%s\n", strFile);

    fclose(f);
    

    //system("pause");
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2020-12-11
@SaNNy32

https://www.geeksforgeeks.org/c-program-replace-wo...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question