D
D
d3coy_002020-12-09 13:15:55
C++ / C#
d3coy_00, 2020-12-09 13:15:55

Editing a C++ file?

Hello, I read the text into the buffer, how can I replace the string in this buffer through the for and if loop and write the buffer to a file?

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

    FILE *f = fopen(sPath_in, "rb");
    fseek(f, 0, SEEK_END);
    long FileLength = ftell(f);
    fseek(f, 0, SEEK_SET);
    
    
    char *string = (char *)malloc(FileLength + 1);
    fread(string, FileLength, 1, f);
    cout << string << endl;
    fclose(f);
    

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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
User700, 2020-12-09
@User700

At the end it should be required to write 0. Malloc does not zero out the allocated memory?
Find substring strstr. If found, copy strcpy to the output buffer, or immediately to file start, then replace, and so on. Then copy the remaining end. When working with a buffer, realloc should be provided when re-floating. If the task is limited to one occurrence, one replacement of the first occurrence, you can initially allocate a buffer of size filesize + newsubstrlen - oldsubstrlen, rewrite (memmov or strmov, if the latter exists) the end of the string and copy the memcpy string to the middle.
Why is the output not in C?

A
Alexander Ananiev, 2020-12-09
@SaNNy32

Your buffer is an array of characters. You can convert this array to another array in any way you like. But if you are using C++ and not C, then use https://en.cppreference.com/w/cpp/io/basic_ifstream
to work with files . Break text into words and store them in vector or list containers . For manipulations with text (insertion, deletion), use the appropriate methods of the vector or list containers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question