How to Overwrite a Text File in C++

Asked 1 years ago, Updated 1 years ago, 343 views

I would like to overwrite the text file while turning the while loop in C++ as follows.
"In the code below, the letter ""text"" is added as ""texttext""."What should I do to overwrite it?

#define TEMP_FILE_NAME "\\tmp.txt"
std::ofstream ofs;

SHGetFolderPath (NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, TmpPath); 
PathAppend(TmpPath,_T(TEMP_FILE_NAME)));

remove(TmpPath);
ofs.open(TmpPath, std::ios::trunc);

while(true)
{
    ofs<<"test";
}

c++

2022-11-26 03:04

3 Answers

Constructor destructors are available in the C++ language, enabling RAII.

Like the question

 std::ofstream ofs;

If you define a variable in a position that is completely different from the actual variable usage timing, the constructor destructor will not work at the expected time.

for(;;){
    std::ofstream ofs;
    ofs.open(TmpPath, std::ios::trunc);
    ofs<<"test";
}

If defined in and where variables are used, ofs will run constructor destructors for each loop, and write and close files every time.

Assuming that it is used in this way, the std::ofstream constructor also has the ability to automatically call open() and

for(;;){
    std::ofstream ofs {TmpPath, std::ios::trunc};
    ofs<<"test";
}

and so on.In this case, the file is opened on the line std::ofstream ofs {TmpPath, std::ios::trunc} and buffer flash and file close on } at the end of the loop.

If you only want to write test,

without any variables.
for(;;){
    std::ofstream {TmpPath, std::ios::trunc}<<"test";
}

You can write .In this case, only this line will close the file open buffer flash file, so the timing of the file operation will be clear even if there are other actions in the loop.

PathAppend(TmpPath,_T(TEMP_FILE_NAME)));
remove(TmpPath);

C++17 provides <filesystem> to enable these path and file operations.

#include<filesystem>
...
auto const path=std::filesystem::path {TmpPath}/TEMP_FILE_NAME;
std::filesystem::remove(path);
for(;;){
    std::ofstream ofs {path, std::ios::trunc};
    ofs<<"test";
}


2022-11-26 06:21

I don't know what I wanted to do with open, but I need to put it in the loop for trunc to work.Also, close is required to reopen.

Therefore, simply the following modifications will be made:

#include<windows.h>
# include<shlobj_core.h>
# include <shlwapi.h>
# include <tchar.h>
# include <fstream>

int main() {
# define TEMP_FILE_NAME "\\tmp.txt"
    std::ofstream ofs;
    TCHAR TmpPath [MAX_PATH];

    ::SHGetFolderPath(NULL,CSIDL_APPDATA|CSIDL_FLAG_CREATE,NULL,0,TmpPath);
    ::PathAppend(TmpPath,_T(TEMP_FILE_NAME));

    _tremove(TmpPath);

    while(ofs)
    {
        ofs.open(TmpPath, std::ios::trunc);
        ofs<<"test";
        ofs.close();
    }
    return 0;
}

*The code is complemented because it doesn't make sense if it doesn't work. (I used _T, so I changed only remove.)


2022-11-27 04:03

You have already approved a highly rated answer, but
If you just want to overwrite it, you can search ofs.seekp to the beginning of the file.

ofs.seekp(0,std::ios_base::beg);
    ofs<<"test";


2022-11-27 09:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.