Thread: unformatted output through operator<<

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    unformatted output through operator<<

    i want to use streams from std lib to output int, long, __int64, etc...
    but i dont want to have them formatted, i want unformatted output. that's no problem useing write(), but if i use operator<<, it get's allways formatted.
    is there a flag to direct to unformatted output or do i have to write my own num_put class ??

    by the way, are there any facilitys to access files apart from formatted streams included in std lib ?
    Teneniel
    "The Frenchmen and Russians possess the land, the British possess the sea, but we have over the airy realm of dreams command indisputably." ~ Heinrich Heine

  2. #2
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    I'm understanding you mean unformatted as in you want it to output hexidecimal?
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but if i use operator<<, it get's allways formatted.
    The << and >> operators are formatted by definition, if you want block input or output, use read and write.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    Wink Thanks,but my real life friend was faster!

    Thank you for your replies. I have allready found out what i needed to know. If someone's interested, here's what I meant:
    Code:
    unsigned long foo = 1234567; 
    myfilestream myfile;
    myfile << foo << " "<< std::hex << foo; // assume myfile to be an open ofstream
    myfile would now contain
    Code:
    "1234567 0012d687" (I don't know if it uses big endian...)
    But that is bad, because sometimes you want to write the data like it appears in system memory.
    You could write instead:
    Code:
    myfile.write(reinterpret_cast<myfilestream::char_type*>(&foo), sizeof(foo));
    /*which produces on big endian machines*/
    "‡Ö "
    But that all wasn't a solution for my problem:
    Code:
    class foo
    { 
      friend istream& operator >> (istream&, foo&);
      /*...*/
      // data members:
      unsigned long _start;
      unsigned long _end;
    };
    // assume using namespace std;
    istream& operator >> (istream& in, foo& f)
    {
      in >> f._start;
      in >> f._end;
      if (f._start > f._end) swap(f._start, f._end);
      return in;
    }
    
    class bar
    { 
      istream& operator >> (istream& in, bar&)
      /*...*/
      // own data
      unsigned long _id;
      foo _location;
    }
    istream& operator >> (istream& in, bar& b)
    {
      in >> b._id;
      in >> f._location; // CALLS operator>>(/*...*/) for _location
      return in;
    }
    foo's operator<< checks for disordered start/end values and swaps them if necessary.
    If i had just used
    Code:
    in.read(reinterpret_cast<istream::char_type*>(&bar), sizeof(bar));
    that check was never performed, because operator >> was never called for _location (which is a foo and has it's own overloaded verson of >>).

    But i can't encode my files in ascii, and streams will allways output as ascii, except:
    The stream's local's facets num_put/num_get are replaced by your own ones, that just perform a raw memory write.
    Those classes perform formattion of numbers. Search your stl / std lib documentation for more information ( header <locale> ).

    That's pretty handy for debugging as well because you can switch between ascii and raw representation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM