Whats wrong with my stream ?

L

lothar.behrens

Hi,

my own stream implementation writes correctly, but it does not read all
back. Why ?

Thanks, Lothar

Here is the output:

'Testdata1: ', 0
'Testdata2: ', 1
'Testdata3: ', 2
'Testdata4: ', 3
'Testdata5: ', 4
'Testdata6: ', 5
'Testdata7: ', 6
'Testdata8: ', 7
'Testdata9: ', 8
'Testdata10: ', 9
'Testdata11: ', 2573
'', 0
'', 0
'', 0
'', 0
'', 0
Here is the code:

void main() {
MyInputStream in = new MyInputStream();
MyOutputStream out = new MyOutputStream();

out->setFileName("Test.txt");
out->open();
int n = 0;
*out << "Testdata1: " << n++;
// repeat14 times
*out << "Testdata16: " << n++;
out->close();
n->setFileName("Test.txt");
in->open();
char* buf = NULL;
for (int i = 0; i < 16; i++) {
n = 0;
*in >> buf >> n;
printf("'%s', %d\n", buf, n);
}
}

bool LB_STDCALL lbInputStream::close() {
if (isOpen) {
fflush(fin);
fclose(fin);
isOpen = false;
}
return true;
}
bool LB_STDCALL lbInputStream::eek:pen() {
fin = fopen(f, "rb");
if (!fin) {
return false;
}
isOpen = true;
return true;
}
lb_I_InputStream& LB_STDCALL lbInputStream::eek:perator>> (int& i) {
if (!isOpen) return *this;
fread(&i, sizeof(i), 1, fin);
return *this;
}
lb_I_InputStream& LB_STDCALL lbInputStream::eek:perator>> (char& c) {
if (!isOpen) return *this;
fread(&c, sizeof(c), 1, fin);
return *this;
}
lb_I_InputStream& LB_STDCALL lbInputStream::eek:perator>> (char*& string)
{
char* buf = NULL;
int size = 0;
fread(&size, sizeof(size), 1, fin);
buf = (char*) malloc(size);
fread(buf, size, 1, fin);
if (string != NULL) free(string);
string = buf;
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream::eek:perator<< (const int i)
{
if (!isOpen) return *this;
fwrite(&i, sizeof(i), 1, fout);
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream::eek:perator<< (const char c)
{
if (!isOpen) return *this;
fwrite(&c, sizeof(c), 1, fout);
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream::eek:perator<< (const char*
string) {
if (!isOpen) return *this;
int len = strlen(string)+1;
fwrite(&len, sizeof(len), 1, fout); // Write len + 1 of string
fwrite(string, len, 1, fout);
return *this;
}
bool LB_STDCALL lbOutputStream::close() {
if (isOpen) {
fflush(fout);
fclose(fout);
isOpen = false;
}
return true;
}
bool LB_STDCALL lbOutputStream::eek:pen() {
fout = fopen(f, "w");
if (!fout) {
return false;
}
isOpen = true;
return true;
}
 
L

Luke Meyers

MyInputStream in = new MyInputStream();
MyOutputStream out = new MyOutputStream();

Where are the definitions of these classes? Why are you attempting to
initialize something of type MyInputStream with a value of type
MyInputStream*? When and how were you planning to delete those
pointers?
out->setFileName("Test.txt");

So now it's a pointer?

etc....

Luke
 
L

lothar.behrens

Yes, they are pointers to my classes. A typo :-(
My orginal code is more complex, so I wrote it as simple instantiation.

Indeed I found what the problem was: I open the output stream with
"wb", but my input stream
with "r". There were other postings on wich I saw a comment relating to
binary files.

Deleting that pointers ?

No, I don't. I have something like smart pointers for it. (Macros)

Now it works. It was a long time since I worked with streams (Pascal)
:)

Thanks, Lothar
 
L

Luke Meyers

Deleting that pointers ?

No, I don't. I have something like smart pointers for it. (Macros)

A macro which behaves like a smart pointer? This I've got to see.
How's the exception-safety?

Luke
 
L

lothar.behrens

I do not yet use exceptions. I know, things may fail, but until yet my
code works
as expected. Here, macros are not the best way, but I didn't have
reviewed equivalent
implementations.

Lothar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top