Skip to content

Commit

Permalink
Write a Byte Order Mark (BOM) to the CSV file.
Browse files Browse the repository at this point in the history
This indicates UTF-8 and thereby fixes special characters when opening the resulting CSV in Excel.
To my knowledge, it has no negative effects in any application that works with CSV files.

Fixes #4
  • Loading branch information
ColinFinck committed Nov 30, 2023
1 parent 82a215b commit 7ad8a49
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/csv_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ ExportCSV(const std::wstring& wstrCSVFilePath, const std::vector<S7DeviceSymbolI
return CS7PError(L"CreateFileW failed with error " + std::to_wstring(GetLastError()));
}

// Write the CSV output.
// Write the BOM to indicate UTF-8 output.
const unsigned char ByteOrderMark[] = { 0xEF, 0xBB, 0xBF };
DWORD cbWritten;
if (!WriteFile(hFile.get(), ByteOrderMark, sizeof(ByteOrderMark), &cbWritten, nullptr))
{
return CS7PError(L"WriteFile failed for the ByteOrderMark with error " + std::to_wstring(GetLastError()));
}

// Write the CSV output.
if (!WriteFile(hFile.get(), strCSV.c_str(), strCSV.size(), &cbWritten, nullptr))
{
return CS7PError(L"WriteFile failed with error " + std::to_wstring(GetLastError()));
return CS7PError(L"WriteFile failed for the CSV content with error " + std::to_wstring(GetLastError()));
}

return std::monostate();
Expand Down

0 comments on commit 7ad8a49

Please sign in to comment.