1
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    std::wfstream file("../Data/books.txt", std::wios::in);
    std::vector<std::wstring> lines;
    std::wstring line;

    while (!file.eof())
    {
        std::getline(file, line);
        lines.push_back(line);
    }

    for (const auto& item : lines)
        std::wcout << item << std::endl;

    return 0;
}

Using that code, I am aiming to read all of the lines from a file and store them in a std::vector.

File has the following content:

Bir Ömür Nasıl Yaşanır
İlber Ortaylı
1
1
1

But when I print the elements of the vector, the only output shown is Bir.

The way for reading from file to std::string is basically this, is it different for std::wstring? If so, how can I do it with std::wstring?

  • 1
    Unrelated: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Aug 05 '21 at 00:52
  • What character encoding is used by the file? Make sure it matches what C++ is using to read it. – user4581301 Aug 05 '21 at 00:54
  • 2
    First off, use `std::wifstream` instead of `std::wfstream`+`std::wios::in`. And second, you will likely have to [`imbue()`](https://en.cppreference.com/w/cpp/io/basic_ios/imbue) an appropriate `locale` into the stream to handle the translation of the file's bytes into `wchar_t` characters. – Remy Lebeau Aug 05 '21 at 01:00
  • Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – prehistoricpenguin Aug 06 '21 at 02:16

0 Answers0