Thread: problem with fout.write: cannot write 0x0A (10 dec)

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    8

    Unhappy problem with fout.write: cannot write 0x0A (10 dec)

    hello, i m trying to write the content of a pointer to a file:


    unsigned char* ds=(unsigned char*)malloc(sizeof(unsigned char)*width*height);

    ofstream fout(outname);

    fout.write(reinterpret_cast<const char*>(&(*ds)), width*height);


    (i tried sizeof(*ds) instead of width*height but it does not work)


    all it does is return a file slightly BIGGER than width*heigth, because of the following issue: the char 0x0A cannot be written, it is replaced by 0x0D0A (that s right, 2 chars for the price of 1)

    can anyone help me on this, please? (no need to mention that i m new to MSVC++ .NET ...)

    thanks a lot!
    -yves-

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Open the file in binary mode to not have the 0x0A automatically converted to 0x0D,0x0A:

    Code:
    ofstream fout(outname,ios::binary|ios::out);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    open the output file as binary

    ofstream fout(outname, ios_base::binary);
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    8
    thanks a lot!

    works like a charm now!!


  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Note that sizeof(unsigned char) is guaranteed to be 1 by the standard.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Oh and why not use new and delete instead of malloc
    Woop?

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    8

    malloc vs new ?

    what would be the advantages of using new instead of malloc?

    i m having some memory leaks problems, could that be due to malloc - is new better in terms of memory management?

    -yves-

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are writing in C++, you should use new/delete - malloc/free does not call constructors or destructors, so you will eventually need new/delete in your C++ code. It makes no sense to use both. There are some advanced reasons to use malloc in a C++ program, but if you don't know what they are you shouldn't be using them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working out
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-29-2003, 01:17 PM
  2. Segmentation Fault
    By Lost__Soul in forum C Programming
    Replies: 46
    Last Post: 04-28-2003, 04:24 AM
  3. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 11:00 AM
  4. Heaps...
    By Nutshell in forum C Programming
    Replies: 14
    Last Post: 04-23-2002, 08:54 AM
  5. Formatting Output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-26-2002, 01:33 AM