Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Whats wrong with my stream ?

Reply
Thread Tools

Whats wrong with my stream ?

 
 
lothar.behrens@lollisoft.de
Guest
Posts: n/a
 
      01-22-2006
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:pen() {
fin = fopen(f, "rb");
if (!fin) {
return false;
}
isOpen = true;
return true;
}
lb_I_InputStream& LB_STDCALL lbInputStream:perator>> (int& i) {
if (!isOpen) return *this;
fread(&i, sizeof(i), 1, fin);
return *this;
}
lb_I_InputStream& LB_STDCALL lbInputStream:perator>> (char& c) {
if (!isOpen) return *this;
fread(&c, sizeof(c), 1, fin);
return *this;
}
lb_I_InputStream& LB_STDCALL lbInputStream: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:perator<< (const int i)
{
if (!isOpen) return *this;
fwrite(&i, sizeof(i), 1, fout);
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream:perator<< (const char c)
{
if (!isOpen) return *this;
fwrite(&c, sizeof(c), 1, fout);
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream: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:pen() {
fout = fopen(f, "w");
if (!fout) {
return false;
}
isOpen = true;
return true;
}

 
Reply With Quote
 
 
 
 
Luke Meyers
Guest
Posts: n/a
 
      01-22-2006
wrote:
> 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

 
Reply With Quote
 
 
 
 
lothar.behrens@lollisoft.de
Guest
Posts: n/a
 
      01-22-2006
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

 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      01-22-2006
wrote:
> 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

 
Reply With Quote
 
lothar.behrens@lollisoft.de
Guest
Posts: n/a
 
      01-23-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DVD with subtitle stream to DivX with same subtitle stream(selectable) malise Software 2 04-17-2007 09:15 AM
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments