Chachmu
Chachmu

Reputation: 8192

How to keep characters in C++ from combining when outputted to text file

I have a fairly simple program with a vector of characters which is then outputted to a .txt file.

ofstream op ("output.txt");
vector <char> outp;  
for(int i=0;i<outp.size();i++){
    op<<outp[i]; //the final output of this is incorrect
    cout<<outp[i]; //this output is correct
}

op.close();

the text that is output by cout is correct, but when I open the text file that was created, the output is wrong with what look like Chinese characters that shouldn't have been an option for the program to output. For example, when the program should output:

O dsof

And cout prints the right output, the .txt file has this:

O獤景

I have even tried adding the characters into a string before outputting it but it doesn't help. My best guess is that the characters are combining together and getting a different value for unicode or ascii but I don't know enough about character codes to know for sure or how to stop this from happening. Is there a way to correct the output so that it doesn't do this? I am currently using a windows 8.1 computer with code::blocks 12.11 and the GNU GCC compiler in case that helps.

Upvotes: 0

Views: 150

Answers (2)

wacky6
wacky6

Reputation: 145

there is nothing wrong with your code.

maybe the text editor you use has a default encoding. use more advanced editors and you will get the right output.

Upvotes: 1

TheUndeadFish
TheUndeadFish

Reputation: 8171

Some text editors try to guess the encoding of a file and occasionally get it wrong. This can particularly happen with very small amounts of text because whatever statistical analysis is being used just doesn't have enough data to make a good conclusion. Window's Notepad has/had an infamous example with the text "Bush hid the facts".

More advanced text editors (for example Notepad++) may either not experience the same problem or may give you options to change what encoding is being assumed. You could use such to verify that the contents of the file are actually correct.

Hex editors/viewers are another way, since they allow you to examine the raw bytes of the file without interpretation. For instance, HxD is a hex editor that I have used in the past.

Alternatively, you can simply output more text. The more there is, generally the less likely something will guess wrong. From some of my experiences, newlines are particularly helpful in convincing the text editor to assume the correct encoding.

Upvotes: 4

Related Questions