 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lothar.behrens@lollisoft. Guest
|
Posted: Sun Jan 22, 2006 10:48 pm Post subject: Whats wrong with my stream ? |
|
|
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', %dn", buf, n);
}
}
bool LB_STDCALL lbInputStream::close() {
if (isOpen) {
fflush(fin);
fclose(fin);
isOpen = false;
}
return true;
}
bool LB_STDCALL lbInputStream::open() {
fin = fopen(f, "rb");
if (!fin) {
return false;
}
isOpen = true;
return true;
}
lb_I_InputStream& LB_STDCALL lbInputStream::operator>> (int& i) {
if (!isOpen) return *this;
fread(&i, sizeof(i), 1, fin);
return *this;
}
lb_I_InputStream& LB_STDCALL lbInputStream::operator>> (char& c) {
if (!isOpen) return *this;
fread(&c, sizeof(c), 1, fin);
return *this;
}
lb_I_InputStream& LB_STDCALL lbInputStream::operator>> (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::operator<< (const int i)
{
if (!isOpen) return *this;
fwrite(&i, sizeof(i), 1, fout);
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream::operator<< (const char c)
{
if (!isOpen) return *this;
fwrite(&c, sizeof(c), 1, fout);
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream::operator<< (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::open() {
fout = fopen(f, "w");
if (!fout) {
return false;
}
isOpen = true;
return true;
}
|
|
| Back to top |
|
 |
Luke Meyers Guest
|
Posted: Sun Jan 22, 2006 10:55 pm Post subject: Re: Whats wrong with my stream ? |
|
|
[email]lothar.behrens (AT) lollisoft (DOT) de[/email] wrote:
| Quote: | 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?
| Quote: | out->setFileName("Test.txt");
|
So now it's a pointer?
etc....
Luke
|
|
| Back to top |
|
 |
lothar.behrens@lollisoft. Guest
|
Posted: Sun Jan 22, 2006 11:22 pm Post subject: Re: Whats wrong with my stream ? |
|
|
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
|
|
| Back to top |
|
 |
Luke Meyers Guest
|
Posted: Sun Jan 22, 2006 11:27 pm Post subject: Re: Whats wrong with my stream ? |
|
|
[email]lothar.behrens (AT) lollisoft (DOT) de[/email] wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|