- 1 year ago
How to make a filestream read in UTF-8 C++
Comment
1 / 1
In C++, you can read a file in UTF-8 encoding using std::ifstream
with std::getline
. To ensure that the file is read in UTF-8, you should also set the proper locale. Here's how you can do it:
cpp#include <iostream>
#include <fstream>
#include <locale>
int main() {
// Set the locale to UTF-8 to handle Unicode characters correctly
std::locale::global(std::locale("en_US.UTF-8"));
// Open the file in binary mode to avoid newline conversion on Windows
std::ifstream file("your_file.txt", std::ios::binary);
if (!file.is_open()) {
std::cout << "Failed to open the file." << std::endl;
return 1;
}
// Read the file line by line in UTF-8
std::string line;
while (std::getline(file, line)) {
// Process the UTF-8 line here
std::cout << line << std::endl;
}
// Close the file when done
file.close();
return 0;
}
Make sure to replace "your_file.txt"
with the path to your UTF-8 encoded file. The std::locale::global(std::locale("en_US.UTF-8"))
line sets the global locale to UTF-8, which allows the file to be read correctly with Unicode characters. Opening the file in binary mode (std::ios::binary
) ensures that newline conversions on Windows are avoided.