String Output to File
So I'm trying to write a large string to a .txt file, but am having some trouble. The string I want to output is this:
0.x.y.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.1.x.y.t.x.y.t.x.y.t.2.x.y.t.s.x.y.t.s.x.y.t.s.x.y.t.s.3.x.y.t.x.y.t.4.N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.x.y.t.x.y.t.N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.5.
(It's a data save format, not worth getting into).
To try to test this, I first wrote this (where chunk
is the above string, created outside the function, that I want to output):
void WriteToFile(std::string chunk)
{
cout << "Writing...\n";
ofstream SaveGame;
SaveGame.open("SaveGame.txt");
std::string MainString = "0.x.y.t.f.t.f.t.f.t.f.";
cout << MainString;
SaveGame << MainString;
cout << "Done!\n";
}
This test code works fine, and my output file contains 0.x.y.t.f.t.f.t.f.t.f.
But when I try this:
void WriteToFile(std::string chunk)
{
cout << "Writing...\n";
ofstream SaveGame;
SaveGame.open("SaveGame.txt");
std::string MainString = chunk;
cout << MainString;
SaveGame << MainString;
cout << "Done!\n";
}
I get gibberish:
⸰⸱⸲⸳⸴⹎慮敭琮昮洮䤮砮礮琮砮礮琮砮礮琮砮礮琮丮渮浡⹉⹎慮敭琮昮洮䤮砮礮琮砮礮琮砮礮琮砮礮琮丮渮浡⹉⸵
Needless to say, this isn't what I want.
Here is the full code of the program; note that it fails whether I use WriteToFile(Chunk) or WriteToFile(Total). The code also fails if I initialize MainString as the entire string within WriteToFile.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void ReadFile();
void WriteToFile(std::string chunk);
int main()
{
///CHUNK SAVE FORMAT
std::string Chunk = "0.";
Chunk += "x.y.";
Chunk += "t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.";
Chunk += "t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.";
Chunk += "t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.";
Chunk += "t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.";
Chunk += "t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.";
Chunk += "t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.";
Chunk += "t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.";
Chunk += "t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.";
Chunk += "1.";
Chunk += "x.y.t.";
Chunk += "x.y.t.";
Chunk += "x.y.t.";
Chunk += "2.";
Chunk += "x.y.t.s.";
Chunk += "x.y.t.s.";
Chunk += "x.y.t.s.";
Chunk += "x.y.t.s.";
Chunk += "3.";
Chunk += "x.y.t.";
Chunk += "x.y.t.";
Chunk += "4.";
Chunk += "N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.";
Chunk += "N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.x.y.t.x.y.t.";
Chunk += "N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.";
Chunk += "N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.";
Chunk += "5.";
std::string Total = "0.x.y.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.t.f.1.x.y.t.x.y.t.x.y.t.2.x.y.t.s.x.y.t.s.x.y.t.s.x.y.t.s.3.x.y.t.x.y.t.4.N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.x.y.t.x.y.t.N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.x.y.t.N.name.t.f.m.I.x.y.t.x.y.t.x.y.t.5.";
WriteToFile(Total);
return 0;
}
void ReadFile()
{
}
void WriteToFile(std::string chunk)
{
cout << "Writing...\n";
ofstream SaveGame;
SaveGame.open("SaveGame.txt");
std::string MainString = chunk;
cout << MainString;
SaveGame << MainString;
cou开发者_如何学编程t << "Done!\n";
}
What's going on here?
Without even having tried it, I can confidently say that it works for me. The error is not in the code but elsewhere:
I suspect that you open the text file in Notepad on Windows, or a similar program. The application will try to guess the file’s encoding and (wrongly) guess that it’s a Unicode-encoded file (UTF-16).
To remedy this, specify an encoding when opening the file (if Notepad doesn’t support this use a proper text editor, such as Notepad++).
The 'gibberish' is the correct data, but interpreted as 16-bit unicode chars. Look at the little hex numbers in the squares:
⸰ is 0x2E and 0x30 ('.' and '0')
My guess is you're writing the file correctly, but somehow inspecting your written data in the wrong manner.
Do you happen to open the file using notepad? If so, notepad uses some heuristics (IsTextUnicode) to guess the encoding of the file's content if there's no BOM. In your case, that particular content makes it think it's actually Unicode...
Your code works fine. It's notepad that's broken. Read more about the notepad encoding problem here.
The code you present works as intended when compiled with g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) Diagnosing your problem will require information about your environment.
To clarify, does the misbehaving code echo gibberish to the terminal, as well as when you view the file?
I suspect that your function is actually generating the bits you want, but they happen to trick something into thinking that the file is unicode and not ascii.
There seems to be some issue in encoding. Try opening the file in editor other than notepad (wordpad or notepad++), also you will get the correct result upon reading the same file.
精彩评论