Streamwriter删除切换文档时的一些行 [英] Streamwriter deletes some lines on switching documents

查看:100
本文介绍了Streamwriter删除切换文档时的一些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





你好,

我在控制台写了一个Prim Number Calculator我想要将prims保存在一些文本文档中。



问题是文本文件的文件大小要打开(500 mb)。

我将它们放入serval文件中,每500个prims后新的txt,



现在这里问题:程序工作和工作,在达到ammount后500,它正在创建新文件,当它发生时,它会消耗掉
2543

2549

2551

2



甚至在新文档中跳过一些,它从3581开始,所以他忽略了大约1000个可能的原始数字。



如果数字n是一个正数,则exe开始计算,如果是,则将其添加到列表中。



结束时它将它写入txt中的exe。



  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]);





我的尝试:



我甚至试图在其中放置一个Thread.sleep,它根本不起作用。



我还有另一个可能的解决方案,我忘记了吗? br />


帮助我解决问题。

解决方案

正如理查德已经提到的那样,你必须在打开之前关闭作者。新的确保所有数据都被写入:

您必须调用Close以确保所有数据都正确写入基础流。



因此修改您的代码:

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

WriteMax + = 500 ;
// 此处关闭上一个文件以确保所有数据都已写入
myWriter 。关();
myWriter = new StreamWriter( PrimUpto + WriteMax + .txt);
}
myWriter.WriteLine(primes [loop]);
}
// 关闭当前(最后)文件
myWriter .Close();




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.

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.

解决方案

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:

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();


这篇关于Streamwriter删除切换文档时的一些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆