# Off-Topic Discussion > The Lounge > Tech Talk >  >  C++ Write to File Problem

## Maria92

Alright, so I'm a newbie in C++, but I found a source code for the Sieve of Eratosthenes, and I'm trying to modify it so that instead of outputting the numbers into the command prompt, it writes them to a file. I can do this...up to 1400 or so. Somewhere between 1400 and 1500, I get a text file containing only random gibberish. Here's the output at 2000:




```
′″‵‷ㄱㄠ″㜱ㄠ㌲㈠ㄳ㌠‷ㄴ㐠″㜴㔠″㤵㘠‱㜶㜠‱㌷㜠㌸㠠㜹ㄠ㄰ㄠ㌰ㄠ㜰ㄠ㤰ㄠ㌱ㄠ㜲ㄠㄳㄠ㜳ㄠ㤳ㄠ㤴ㄠㄵㄠ㜵ㄠ㌶ㄠ㜶ㄠ㌷ㄠ㤷ㄠㄸㄠㄹㄠ㌹ㄠ㜹ㄠ㤹㈠ㄱ㈠㌲㈠㜲㈠㤲㈠㌳㈠㤳㈠ㄴ㈠ㄵ㈠㜵㈠㌶㈠㤶㈠ㄷ㈠㜷㈠ㄸ㈠㌸㈠㌹㌠㜰㌠ㄱ㌠㌱㌠㜱㌠ㄳ㌠㜳㌠㜴㌠㤴㌠㌵㌠㤵㌠㜶㌠㌷㌠㤷㌠㌸㌠㤸㌠㜹㐠㄰㐠㤰㐠㤱㐠ㄲ㐠ㄳ㐠㌳㐠㤳㐠㌴㐠㤴㐠㜵㐠ㄶ㐠㌶㐠㜶㐠㤷㐠㜸㐠ㄹ㐠㤹㔠㌰㔠㤰㔠ㄲ㔠㌲㔠ㄴ㔠㜴㔠㜵㔠㌶㔠㤶㔠ㄷ㔠㜷㔠㜸㔠㌹㔠㤹㘠㄰㘠㜰㘠㌱㘠㜱㘠㤱㘠ㄳ㘠ㄴ㘠㌴㘠㜴㘠㌵㘠㤵㘠ㄶ㘠㌷㘠㜷㘠㌸㘠ㄹ㜠㄰㜠㤰㜠㤱㜠㜲㜠㌳㜠㤳㜠㌴㜠ㄵ㜠㜵㜠ㄶ㜠㤶㜠㌷㜠㜸㜠㜹㠠㤰㠠ㄱ㠠ㄲ㠠㌲㠠㜲㠠㤲㠠㤳㠠㌵㠠㜵㠠㤵㠠㌶㠠㜷㠠ㄸ㠠㌸㠠㜸㤠㜰㤠ㄱ㤠㤱㤠㤲㤠㜳㤠ㄴ㤠㜴㤠㌵㤠㜶㤠ㄷ㤠㜷㤠㌸㤠ㄹ㤠㜹ㄠ〰〱㌱ㄠ㄰〱ㄲㄠ㌰‱〱㌳ㄠ㌰〱㤴ㄠ㔰‱〱ㄶㄠ㘰″〱㤶ㄠ㠰‷〱ㄹㄠ㤰″〱㜹ㄠ〱″ㄱ㤰ㄠㄱ‷ㄱ㌲ㄠ㈱ㄱㄵㄠ㔱″ㄱ㌶ㄠ㜱‱ㄱㄸㄠ㠱‷ㄱ㌹ㄠ〲‱㈱㌱ㄠㄲ‷㈱㌲ㄠ㈲㈱ㄳㄠ㌲‷㈱㤴ㄠ㔲㈱㜷ㄠ㜲㈱㌸ㄠ㠲㈱ㄹㄠ㤲‷㌱㄰ㄠ〳″㌱㜰ㄠㄳ㌱ㄲㄠ㈳‷㌱ㄶㄠ㘳‷㌱㌷ㄠ㠳‱㌱㤹ㄠ〴㐱㌲ㄠ㈴‷㐱㤲ㄠ㌴″㐱㤳ㄠ㐴‷㐱ㄵㄠ㔴″㐱㤵ㄠ㜴‱㐱ㄸㄠ㠴″㐱㜸ㄠ㠴㐱㌹ㄠ㤴㔱ㄱㄠ㈵″㔱ㄳㄠ㐵″㔱㤴ㄠ㔵″㔱㤵ㄠ㘵‷㔱ㄷㄠ㜵㔱㌸ㄠ㤵‷㘱㄰ㄠ〶‷㘱㤰ㄠㄶ″㘱㤱ㄠ㈶‱㘱㜲ㄠ㌶‷㘱㜵ㄠ㘶″㘱㜶ㄠ㘶㘱㌹ㄠ㤶‷㘱㤹ㄠ〷㜱ㄲㄠ㈷″㜱㌳ㄠ㐷‱㜱㜴ㄠ㔷″㜱㤵ㄠ㜷‷㜱㌸ㄠ㠷‷㜱㤸ㄠ〸‱㠱ㄱㄠ㈸″㠱ㄳㄠ㐸‷㠱ㄶㄠ㘸‷㠱ㄷㄠ㜸″㠱㜷ㄠ㜸㠱㤸ㄠ〹‱㤱㜰ㄠㄹ″㤱ㄳㄠ㌹″㤱㤴ㄠ㔹‱㤱㌷ㄠ㜹㤱㜸ㄠ㤹″㤱㜹ㄠ㤹
```


I have no idea how to fix this, and if anyone could help, it would be much appreciated.  :smiley: 




```
// Sieve of Eratosthenes //

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    ofstream myfile;
    myfile.open ("primes.txt", ios::binary);
    
    // Create a sieve of ints, initially set to 1.
    int sievesize;
    cout << "Integer to calculate up to: ";
    cin >> sievesize;
    vector<int, allocator<int> >
        sieve ((size_t)sievesize, 1);

    // At positions that are multiples of i, set value to zero.
    for (int i = 2; i * i < sievesize; i++)
        if (sieve[i])
            for (int j = i + i; j < sievesize; j += i)
                sieve[j] = 0;

    // Now output all the values that are still set.
    for (int j = 2; j < sievesize; j++)
        if (sieve[j]) 
            myfile << j << " ";
      
    return 0;
}
```

----------


## Replicon

You probably don't want that ios::binary in there. Try without it (i.e. text mode) first.

Or just output to stdout and redirect output using ">" on the command line.

----------


## Maria92

It runs the same, with or without ios::binary. And I don't seem to be able to output with cout, then send the output to a file.

----------


## ninja9578

Streams have a bug in GCC 4.2, they simply don't work with numeric values, and create random crapola like that.  You have to either use a different compiler version (4.4 or 4.0.)  Well... you can still use the 4.2 compiler, but you need an updated STL.  It works correctly on both 4.0 and 4.4, I just tested it.

Replicon is incorrect about ios::binary.  The fact is, binary files and text files work exactly the same way, as it's the operating system that handles actual io, and the UNIX standard (and adopted by Linux) only has one type of io.  You should only use something other that binary on Windows or embedded systems that aren't *nix based.

But what replicon means is you can use count and CALL your program using myapp > out.txt.  That's a standard unix function, it has nothing to do with programming.

----------


## Replicon

Come to think of it, you're right. I remember back when FTP was all the rage, and I'd always set the mode to binary to prevent the OS from screwing up binary files with additional newlines and stuff. I figured maybe a stream in binary mode would literally write the binary data out as is (as opposed to converting it to text), but looks like that isn't the case. Heh if there's a bug in streams, well that answers that.  ::lol::

----------


## Maria92

Ah, thank you! I installed MinGW and compiled with that, and the program works wonderfully now. Thanks a million!  ::content::

----------

