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:
Code:
′″‵‷ㄱㄠ″㜱ㄠ‹㌲㈠‹ㄳ㌠‷ㄴ㐠″㜴㔠″㤵㘠‱㜶㜠‱㌷㜠‹㌸㠠‹㜹ㄠㄠ㌰ㄠ㜰ㄠ㤰ㄠ㌱ㄠ㜲ㄠㄳㄠ㜳ㄠ㤳ㄠ㤴ㄠㄵㄠ㜵ㄠ㌶ㄠ㜶ㄠ㌷ㄠ㤷ㄠㄸㄠㄹㄠ㌹ㄠ㜹ㄠ㤹㈠ㄱ㈠㌲㈠㜲㈠㤲㈠㌳㈠㤳㈠ㄴ㈠ㄵ㈠㜵㈠㌶㈠㤶㈠ㄷ㈠㜷㈠ㄸ㈠㌸㈠㌹㌠㜰㌠ㄱ㌠㌱㌠㜱㌠ㄳ㌠㜳㌠㜴㌠㤴㌠㌵㌠㤵㌠㜶㌠㌷㌠㤷㌠㌸㌠㤸㌠㜹㐠㐠㤰㐠㤱㐠ㄲ㐠ㄳ㐠㌳㐠㤳㐠㌴㐠㤴㐠㜵㐠ㄶ㐠㌶㐠㜶㐠㤷㐠㜸㐠ㄹ㐠㤹㔠㌰㔠㤰㔠ㄲ㔠㌲㔠ㄴ㔠㜴㔠㜵㔠㌶㔠㤶㔠ㄷ㔠㜷㔠㜸㔠㌹㔠㤹㘠㘠㜰㘠㌱㘠㜱㘠㤱㘠ㄳ㘠ㄴ㘠㌴㘠㜴㘠㌵㘠㤵㘠ㄶ㘠㌷㘠㜷㘠㌸㘠ㄹ㜠㜠㤰㜠㤱㜠㜲㜠㌳㜠㤳㜠㌴㜠ㄵ㜠㜵㜠ㄶ㜠㤶㜠㌷㜠㜸㜠㜹㠠㤰㠠ㄱ㠠ㄲ㠠㌲㠠㜲㠠㤲㠠㤳㠠㌵㠠㜵㠠㤵㠠㌶㠠㜷㠠ㄸ㠠㌸㠠㜸㤠㜰㤠ㄱ㤠㤱㤠㤲㤠㜳㤠ㄴ㤠㜴㤠㌵㤠㜶㤠ㄷ㤠㜷㤠㌸㤠ㄹ㤠㜹ㄠ〰‹〱㌱ㄠ‹〱ㄲㄠ㌰‱〱㌳ㄠ㌰‹〱㤴ㄠ㔰‱〱ㄶㄠ㘰″〱㤶ㄠ㠰‷〱ㄹㄠ㤰″〱㜹ㄠ〱″ㄱ㤰ㄠㄱ‷ㄱ㌲ㄠ㈱‹ㄱㄵㄠ㔱″ㄱ㌶ㄠ㜱‱ㄱㄸㄠ㠱‷ㄱ㌹ㄠ〲‱㈱㌱ㄠㄲ‷㈱㌲ㄠ㈲‹㈱ㄳㄠ㌲‷㈱㤴ㄠ㔲‹㈱㜷ㄠ㜲‹㈱㌸ㄠ㠲‹㈱ㄹㄠ㤲‷㌱ㄠ〳″㌱㜰ㄠㄳ‹㌱ㄲㄠ㈳‷㌱ㄶㄠ㘳‷㌱㌷ㄠ㠳‱㌱㤹ㄠ〴‹㐱㌲ㄠ㈴‷㐱㤲ㄠ㌴″㐱㤳ㄠ㐴‷㐱ㄵㄠ㔴″㐱㤵ㄠ㜴‱㐱ㄸㄠ㠴″㐱㜸ㄠ㠴‹㐱㌹ㄠ㤴‹㔱ㄱㄠ㈵″㔱ㄳㄠ㐵″㔱㤴ㄠ㔵″㔱㤵ㄠ㘵‷㔱ㄷㄠ㜵‹㔱㌸ㄠ㤵‷㘱ㄠ〶‷㘱㤰ㄠㄶ″㘱㤱ㄠ㈶‱㘱㜲ㄠ㌶‷㘱㜵ㄠ㘶″㘱㜶ㄠ㘶‹㘱㌹ㄠ㤶‷㘱㤹ㄠ〷‹㜱ㄲㄠ㈷″㜱㌳ㄠ㐷‱㜱㜴ㄠ㔷″㜱㤵ㄠ㜷‷㜱㌸ㄠ㠷‷㜱㤸ㄠ〸‱㠱ㄱㄠ㈸″㠱ㄳㄠ㐸‷㠱ㄶㄠ㘸‷㠱ㄷㄠ㜸″㠱㜷ㄠ㜸‹㠱㤸ㄠ〹‱㤱㜰ㄠㄹ″㤱ㄳㄠ㌹″㤱㤴ㄠ㔹‱㤱㌷ㄠ㜹‹㤱㜸ㄠ㤹″㤱㜹ㄠ㤹‹
I have no idea how to fix this, and if anyone could help, it would be much appreciated. 
Code:
// 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;
}
Bookmarks