Whats wrong with my stream ?

Discussion in 'C++' started by lothar.behrens@lollisoft.de, Jan 22, 2006.

  1. Guest

    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;
    }
     
    , Jan 22, 2006
    #1
    1. Advertising

  2. Luke Meyers Guest

    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
     
    Luke Meyers, Jan 22, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , Jan 22, 2006
    #3
  4. Luke Meyers Guest

    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
     
    Luke Meyers, Jan 22, 2006
    #4
  5. Guest

    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
     
    , Jan 23, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre Martins

    Whats wrong with this provider ??

    Alexandre Martins, Aug 14, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    373
    Guenther Liebowitz
    Aug 14, 2003
  2. aa

    Whats wrong with numbers

    aa, Feb 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    316
    Patrice Scribe
    Feb 6, 2004
  3. sviau

    whats wrong with this header???

    sviau, Feb 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    596
    mikeb
    Feb 6, 2004
  4. aa

    Whats wrong

    aa, May 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,597
    Kevin Spencer
    May 20, 2004
  5. aa

    Whats wrong

    aa, May 26, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    348
    Natty Gur
    May 26, 2004
Loading...

Share This Page