Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    cout << "Done!\n";
}

What's going on here?

like image 968
GarrickW Avatar asked Dec 12 '25 16:12

GarrickW


1 Answers

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++).

like image 55
Konrad Rudolph Avatar answered Dec 14 '25 05:12

Konrad Rudolph