- I have two text files that I read, and combine into one data structure.
- I then take the combined data structure and output it to the console using
std::cout
. - I redirect stdout to file
main.exe > my_log.txt
However, for some reason I am getting really weird data output, and I am guessing its something to with the stream/stdout.
Here is what I am getting on output:
- The first character(s) in the stream is always ÿþ
- Every single character has a 'hidden'/extra '\0' in between every character. Even though I am outputting integers.
One of the input files could also contain the weird characters/behavior So I am thinking the reading of this file might be changing something.
Any ideas on what is the problem?
Here is example code of the exact process I am doing (some checks removed)
#include <fstream>
#include <vector>
struct MyStruct {
int a;
int b;
int c;
};
std::vector<MyStruct> all_structs;
int main() {
// I do the exact same process for the two input files
std::ifstream my_file("path/to/file", std::ios::in);
while (!my_file.eof()) {
std::string line;
std::getline(my_file, line);
MyStruct tmp_struct;
tmp_struct.a = std::stoi(line.substr(0, 4));
// Repeat for fields b and c
all_structs.push_back(tmp_struct);
}
// Now output to console
// I have tried using std::cout, printf(), converting to char array using sprintf() and then
// printing. All the same.
for (auto& s : all_structs) {
std::cout << s.a << "," << s.b << "," << s.c << std::endl;
}
}
Edit: I appreciate all the comments and suggestions. I am getting suggestions that we close the question due to duplicates. I would argue that the 'duplicate question' is the solution to this question. The questions are completely different. And any programmer who encounters this problem might not know to search 'Powershell BOM UTF16'.