1

We are using C++ on windows and Linux and UNIX and are reading a text file. On Windows with some files we are getting extra spaces between characters in a line. I have seen an article that seems to explain this but we are getting errors.

C++ getline adding spaces

When we read some files created by applications on Windows there is a space between characters. When we create a text file in Windows it does not have extra spaces.

fstream file;
string fileline;

file.open(configuration_file, ios::in|ios::out);

// This line was added from the post and we get errors

file.imbue(std::locale(file.getloc(), new std::codecvt_utf16<char, 0x10FFFF, std::consume_header>));

if (!file){
    print_progress("Configuration File does not exist\n");
}
else {
    while(!file.eof()) {
        getline(file, fileline);

        std::cout << std::string(fileline) + "\n";
    }
}
file.close();

How do we resolve this in C++? Is there a library that manages this?

Many thanks

AndrewK
  • 31
  • 7
  • This issue (extra spaces) is caused by the *encoding* of the file being something different than you expected. Unfortunately there is no 100% reliable method of knowing a files encoding. As a generalisation Linux tends to use UTF-8 but the situation is more variable on Windows. You might see UTF-16, you might see Windows specific encodings. Basically you need to know externally to your program what the encoding is (ie get your users to tell you want encoding to expect) and then read as appropriate for the encoding you've been told. – john Jun 14 '22 at 19:24
  • Some helpful files may have identifying information, like the UTF [Byte Order Mark](https://en.wikipedia.org/wiki/Byte_order_mark), at the start of the file, but this can't be guaranteed. – user4581301 Jun 14 '22 at 19:45
  • On a side note: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/) – Remy Lebeau Jun 14 '22 at 20:31
  • I had the same problem and solve it by by changing the encoding to utf8 (using Notepad++), you can see my answer her : https://stackoverflow.com/a/73952980/3429103 – ibra Oct 04 '22 at 19:52

0 Answers0