Jennifer84 0 Posting Pro

Hello

I read in alot of text into a Memorystream.

Now I have to find a way to write this stream a chunk at a time to the text file. I have outlined the code that will not work in this case.

As seen I do read in chunks of 1024 bytes into the buffer. Now I need to convert this buffer to a string and use the .Write method. I cant .Write buffer to the file as the parameter .Write in this class doesn´t support it.

How can I complete this code to write the buffer to the textfile?

dynamic OutPut = fso2->OpenTextFile("C:\\test.txt", 2, true);

MemoryStream^ stream = gcnew MemoryStream();
StreamWriter^ WriteFile = gcnew StreamWriter(stream); //Write to stream

//Write the list to the stream
for (int i3 = 0; i3 < 10000000; i3++)
{
    WriteFile->WriteLine("Alot of text here!!!");
}



int READ_CHUNK = 1024 * 1024;
int WRITE_CHUNK = 1024 * 1024;
byte[] buffer = gcnew byte[READ_CHUNK];
int to_write = 0;
int read = 0;

while (true)
{
    read = stream->Read(buffer, 0, READ_CHUNK); //Get a chunk at a time until buffer is <= 0(empty)
    if (read <= 0)
    {
        break;
    }
    String^ s1 = //(How to get a string here from the buffer above ?)
    OutPut->Write(s1);
    //to_write = read; //Update value of to_write

    //while (to_write > 0) //Write to file in chunks as long as there is data left.
    //{
    //    OutPut.Write(buffer, 0, Math.Min(to_write, WRITE_CHUNK));
    //    to_write -= Math.Min(to_write, WRITE_CHUNK);
    //}
}
OutPut->Close();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.