I read a text file and it works very well.
std::string str_buf = "";
FILE *file = fopen("/home/pi/log_2019-03-07.txt", "r");
if (file != NULL)
{
while (true)
{
char buffer[MAX_BUFFER_SIZE] = { 0x00, };
size_t rSize = fread(buffer, MAX_BUFFER_SIZE, sizeof(char), file);
str_buf += buffer;
if (rSize == 0)
break;
}
printf("%s", str_buf.data());
fclose(file);
}
Then I try to write it to same path, another name. This is the code:
FILE *writefile = fopen("/home/pi/WriteTest.txt", "wb");
if (writefile != NULL)
{
int offset = 0;
while (true)
{
size_t wSize = fwrite(&str_buf.data()[offset], sizeof(char), strlen(str_buf.data()) - 1, writefile);
offset += (int)wSize;
if (offset >= strlen(str_buf.data()))
break;
}
fclose(writefile);
}
If I try to execute this code, it works. I open WriteTest.txt
, it has same string. It's perfectly same.
But I found WriteTest.txt
's volume is almost 2 twice read text.
Why it happens?