Thread: write() function

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    write() function

    I managed to write type of string as following
    Code:
    ofstream& operator<<(ofstream& stream, const Data& d) {
        int len;
        char *str;
        str = new char[80];
        string name = "me";
    
        len = static_cast<int>(d.name.length());
        strncpy(str, name.c_str(), len);
        stream.write(reinterpret_cast<const char *>(&len), sizeof(int));
        stream.write(reinterpret_cast<const char *>(str), len);
        return stream;
    
        len = static_cast<int>(sizeof(d.num));
        stream.write(reinterpret_cast<const char *>(&len), sizeof(d.num));
        stream.write(reinterpret_cast<const char *>(d.num), len);
    }
    The overloaded operator above is supposed to work for:
    outFile << Data;
    But I cant get the d.num to print.

    any clue??
    Last edited by alphaoide; 10-02-2003 at 10:59 AM.

  2. #2
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I can't understand what you are trying to do, however, using reinterpret cast to make an int a char pointer is a very bad idea to my mind... There's cases where it might be useful anyway.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I complete the codes above for clarity (see 1st post). So, how would you write an integer using above method?
    edit: the method above write the length field followed by the content
    Last edited by alphaoide; 10-02-2003 at 11:06 AM.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    My question is still what are you trying to do with all that code. Normally, when just outputting, you'd do:

    stream << d.name << " " << d.num << endl;

    and it would work fine. What are you trying to do with the casts and sizeof stuff?

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Ok, just saw your edit. So you are trying to output the length of the field as in how many characters will be printed out, followed by the field itself?

    Maybe convert the int to a string using a stringstream and then do your little thing for that string.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Ok, here's my suggested version. You must #include <sstream>.
    Code:
    ofstream& operator<<(ofstream& stream, const Data& d) {
    
        stream << d.name.length() << d.name;
    
        // Convert int to string using string stream.
        stringstream numStream;
        numStream << d.num;
    
        stream << numStream.str().length() << numStream.str();
    
        return stream;
    }

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Thanks, works nicely.

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Okay, if you're still with me. I managed to write both type of string and of integer. I figured how to read back the type of string, but came up with nothing for type of integer after hours. Any idea
    Code:
        // codes to write string d.name
        len = static_cast<int>(d.name.length());
        strncpy(str, name.c_str(), len);
        stream.write(reinterpret_cast<const char *>(&len), sizeof(int));
        stream.write(reinterpret_cast<const char *>(str), len);
        return stream;
        
        //codes to read string d.name and set it
        stream.read(reinterpret_cast<char *>(&len), sizeof(int));
        stream.read(reinterpret_cast<char *>(strTmp), len);
        strTmp[len] = '\0';
        d.setName(strTmp);
    
        // codes to write int d.num
        outStream << d.num;
        len = static_cast<int>(outStream.str().length());
        strncpy(str, outStream.str().c_str(), len);
        stream.write(reinterpret_cast<const char *>(&len), sizeof(int));
        stream.write(reinterpret_cast<const char *>(str), len);
        outStream.str("");
    
        // codes to read int d.num back????
    Note that I use write() because I write the file in binary mode

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Nevermind! I figured it out. I use atoi() to convert char * to int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM