OVERVIEW
In this section we look at stream classes. wxWidgets has a good number of stream classes for raw input, output, file handling, text handling and archive formats. Stream classes are meant to be used with each other for the particular task at hand.
In the same code below, we create a text file, then we create a zip file and add the text file to the zip file.
SAMPLE CODE
streams.cpp
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#include <wx/zipstrm.h>
int main(int argc, char **argv) {
wxFFileOutputStream * fout = new wxFFileOutputStream(wxT("data.txt"), wxT("w"));
if (!fout->IsOk()) {
wxPuts(wxT("Error - could not create data.txt"));
return -1;
}
wxTextOutputStream *tout = new wxTextOutputStream(*fout);
wxString row = wxT("");
wxString data = wxT("");
for(int i =0; i < 100; i++) {
row = "this is line number ";
row.Append(wxString::Format(wxT("%d"), i));
tout->WriteString(row);
tout->PutChar(wxT('\n'));
data.Append(row).Append(wxT('\n'));
}
fout->Close();
fout = new wxFFileOutputStream(wxT("data.txt.zip"));
wxZipOutputStream *zout = new wxZipOutputStream(*fout);
if (!zout->IsOk()) {
wxPuts(wxT("Error creating data.txt.zip"));
return -1;
}
tout = new wxTextOutputStream(*zout);
zout->PutNextEntry(wxT("data.txt"));
tout->WriteString(data);
zout->Close();
wxPuts(wxT("Done"));
}
The output is shown below
Leave a Reply