Question

So I'm trying to design a program that inputs a file and then reads through the lines and takes each line and outputs info about it to a new file.

I have it all down... except! All my .txt files are filled with garbage instead of what they should be filled with.

I can not figure it out. If I cout the string I'm feeding into the ofstream, the right stuff prints on the screen.

fstream lineOutFile;
string newFileName;
stringstream linefile;

linefile << lineCt;
linefile >> newFileName;

newFileName += ".txt";

lineOutFile.open(newFileName.c_str());

if(!lineOutFile)
{
    cerr << "Can't open output file.\n";
}

stringstream iss;
iss << "The corrected 5' x 3' complement of line " << lineCt <<
    " is as follows - \n\n" << finalSeq << "\n\n\n\n" <<
    "This line of DNA sequence is made up of " << cgContent <<
    " C and G neucleotides.\n\n" << "It contains " << polyTCount <<
    " Poly-T strings of more than 4 consecutive neucleotides. They are as follows. - \n" <<
    polyTString << "\n\n There are " << cpgCount <<
    " CpG sites. The locations are as follows - \n" << cpgString;

string storage;
storage = iss.str();
cout << storage;
lineOutFile << storage;
lineOutFile.close();
lineCt++;

}

I'm getting "⁥潣牲捥整⁤✵砠㌠‧" << This sort of craziness in my .txt files.

I get the right thing when I cout out the same sting immediately before!

Why are my .txt files garbage?

Was it helpful?

Solution

Why are you using fstream instead of ofstream?

The default for fstream is to open an existing file. The default for ofstream is to start with an empty file. Whatever is already in the file is probably causing your editor to interpret the data with the wrong encoding.

OTHER TIPS

Try running

LANG=C your_program

And turn off encoding recognition in your editor. Even better - view your txt files with cat program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top