File streaming FileInputStream/FileOutputStream


#1

Hi all,

I am trying to read a poor text file  and write it back into another file, by using the FileInputStream/FileOutputStream classes.

But when i am trying to write its contents to the other file, it seems to do nothing.

Any help on what is wrong in the following code snippet ? 

void Cpy::readWriteTxtFile()
{
    File InFile("StreamInFile.txt");
    FileInputStream InStream(InFile);
    if (InStream.openedOk())
        String InString = InStream.readEntireStreamAsString();   
    File OutFile("StreamOutFile.txt");
    FileOutputStream OutStream(OutFile);
    if (OutStream.openedOk())
        int    br = OutStream.writeFromInputStream(InStream, InStream.getTotalLength());  //this proccess seems to do nothing
}
 

 

Thanks in advance

 

George 


Using file as synth sound sample
#2

Remove: 

    if (InStream.openedOk())

        String InString = InStream.readEntireStreamAsString();

And I think the file pointer will be at the beginning still and then your copy will work..

Or add:

    InStream.setPosition(0);

Or close and re-open the InputStream.


#3

Thanks for your help.

with InStream.setPosition(0) it works fine.  "Elementary my dear Watson" !!

One more question : how can I close and re-open any fileStream into the same function?

There are no functions as FileInputStream.open() or close().

Open and close happens in the constructor and destructor respectively.

BTW Jules, the writeFromInputStream() can write an ammount of data up to int64 in size, but the returning number of written bytes is much smaller (a simlpe int). 


#4

You just delete it and open a new stream - the stream classes are designed with to be used in RAII style.

And thanks for the heads-up about the return type of writeFromInputStream, I'll update that!