Click here to Skip to main content
15,918,617 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:


Hi there,
I wrote a Prim Number Calculator in the console and i want to save the prims in some textdocuments.

The problem is that those files are getting to huge to open ( 500 mb ) for a textdocument.
I startet to put them into serval documents, after every 500 prims a new txt,

Now here the Problem: the program works and works, after reaching the ammount of 500, it is creating the new file, when it does, it sops at
2543
2549
2551
2

it even skips some more in the new doc, it starts with 3581 so he ignored about 1000 possible prim numbers.

The exe starts with calculating if number n is a prim, if ist true, ist adding it onto a list.

at the end of the exe it stars writing it into the txt.

C#
int WriteMax = 500;
for (int loop= 0; loop< primes.Count; loop++)
{
    if(WriteMax == Schleife)
    {

        WriteMax += 500;
        myWriter = new StreamWriter("PrimUpto"+WriteMax+".txt");
    }
    myWriter.WriteLine(primes[loop]);


What I have tried:

I even tried to put a Thread.sleep in it, which didnt work at all.

I there another possible solution which i forgot there?

Thx for helping me.
Posted
Updated 15-Apr-16 1:28am
Comments
Sinisa Hajnal 15-Apr-16 6:56am    
Show more of the code. Where are you filling primes array? How do you empty it? What does the condition (if WriteMax == Schleife) do? Are you flushing/disposing StreamWriter before creating another?
Member 12461252 15-Apr-16 7:04am    
(if WriteMax == Schleife) is equal to (if WriteMax == loop)
i messed up changing it there before uploading the Problem

StreamWriter myWriter = new StreamWriter("PrimUpro500.txt");

for (ulong n = 5; n <= limit; n += 2)
if (myBool[(int)n])
primes.Add(n);
Richard MacCutchan 15-Apr-16 7:14am    
500Mb, how many primes have you generated? You should close the existing stream when you switch to a new one. And what is the value of Schleife; why not just if ((loop % 500) == 0) ?
Member 12461252 15-Apr-16 7:22am    
The 500 mb wherent all of them, i stoped the Progress to check if ist working, but i checked about 1 Billion, but i found the Problem already, thx for that

1 solution

As already mentioned by Richard you must close the writer before opening a new one to ensure that all data are written:
You must call Close to ensure that all data is correctly written out to the underlying stream.

So modify your code:
C#
int WriteMax = 500;
for (int loop= 0; loop< primes.Count; loop++)
{
    if(WriteMax == Schleife)
    {
    
        WriteMax += 500;
        // Close previous file here to ensure that all data are written
        myWriter.Close();
        myWriter = new StreamWriter("PrimUpto"+WriteMax+".txt");
    }
    myWriter.WriteLine(primes[loop]);
}
// Close current (last) file
myWriter.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900