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:
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;
}
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:
fin = fopen(f, "rb");
if (!fin) {
return false;
}
isOpen = true;
return true;
}
lb_I_InputStream& LB_STDCALL lbInputStream:
if (!isOpen) return *this;
fread(&i, sizeof(i), 1, fin);
return *this;
}
lb_I_InputStream& LB_STDCALL lbInputStream:
if (!isOpen) return *this;
fread(&c, sizeof(c), 1, fin);
return *this;
}
lb_I_InputStream& LB_STDCALL lbInputStream:
{
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:
{
if (!isOpen) return *this;
fwrite(&i, sizeof(i), 1, fout);
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream:
{
if (!isOpen) return *this;
fwrite(&c, sizeof(c), 1, fout);
return *this;
}
lb_I_OutputStream& LB_STDCALL lbOutputStream:
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:
fout = fopen(f, "w");
if (!fout) {
return false;
}
isOpen = true;
return true;
}