Problem with .write() function

This is a discussion on Problem with .write() function within the C++ Programming forums, part of the General Programming Boards category; Hi dudes. For some reason this works: Code: ofstream out("filename.dat"); char x[3]="ol"; out.write(x, 1); and this doesnt: Code: ofstream out("filename.dat"); ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    84

    Problem with .write() function

    Hi dudes.

    For some reason this works:

    Code:
        ofstream out("filename.dat");
        char x[3]="ol";
        out.write(x, 1);
    and this doesnt:

    Code:
        ofstream out("filename.dat");
        unsigned char x[3]="ol";
        out.write(x, 1);
    and this second code returns me the folowing error: invalid conversion from 'unsigned char *' to 'const char*'

    but i dont know why, why cant i use an unsigned char instead of a char. and even in some books the examples use an unsigned char.

    Im using the correct headers:
    Code:
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    any ideas??

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    The first argument to the write method of ofstream is a const char *; therefore if you want to write data that is NOT a const char *, you must force it to be a const char *.

    Code:
    ofstream out("filename.dat");
    unsigned char x[]="ol";
    out.write(reinterpret_cast<const char *>(x), 1);

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I thought that wouldn't matter.

    The const here means write() promises it won't change the string. It's totally fine if your original string is non-const.

    Why not just use char?

    Unsigned/signed chars only make sense when you are using them to store numbers. For characters, the default signedness will always do the right thing.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    84
    Yes i know i can use just char, but what really drives me insane is that they use it in tutorials and books, then why doesnt it work for me?? for example, my code was taken from an example in a book.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by shiroaisu View Post
    Yes i know i can use just char, but what really drives me insane is that they use it in tutorials and books, then why doesnt it work for me?? for example, my code was taken from an example in a book.
    Are you sure [it was like that in the book]? In that case, throw away the book, it's crap. You can't do that. Also, mention what book it is. Let's make a "C books not to buy" thread..

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,708
    You should check the book's errata webpage, especially after you notice or otherwise encounter errors in book examples.

    Not to argue with the idea of a bad books thread, but there are good, instructive books with errors, and bad ones with inexcusable problems.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    It's better to not use write at all. You have the stream operators, so it's a good idea to actually use them instead. It will ensure portable code and not having to bother with casts.
    The the thing that the write function needs a cast is to discourage its use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    It's better to not use write at all.
    Unless you are using a stream in binary mode.

    The the thing that the write function needs a cast is to discourage its use.
    >_<

    Could you not do that? If this poor newbie takes such complete crap to heart he'll have a bad start when he wants to operate on binary files.

    Soma

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Fine, fine. If you need to operate with binary files, then you could use it. Or you could even use the C function fwrite/fread instead which offer equivalent functionality and are less painful to use. The C++ write/read offers no advantage whatsoever over its C sibling. In fact, it's more cumbersome instead.
    The only advantage is the iostream. Files will close automatically. But with some RAII, the C fopen/fclose would be no problem either.
    Whopeedo!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    o_O

    So your argument for dishing out such trash is that one could use something different?

    *shrug*

    Okay. I can dig it.

    It's better to not use C++ at all. You have the Ruby* language, so it's a good idea to actually use Ruby. It will ensure portable code without having to bother with casts.

    The thing is that the template functions cause "bloat" to discourage their use.

    The C++ formatted IO manipulators offer no advantage whatsoever over its C siblings. In fact, it's more cumbersome.

    But with some manual labour, the inefficiencies of a system can be fixed or mitigated.

    Awesome.

    Soma

    * picked randomly

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Argument? Not at all.
    I decomposed the don't use write into two sides of an argument due to your request.
    Now that you've seen both sides, you can pick whatever you want.
    Plus binary files aren't portable.

    Now choose! Choose the tool you want or need! Choose! Choose!
    Or don't. That's up to you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    Plus binary files aren't portable.
    O_o

    WTF!?

    Plus binary files aren't portable.
    o_O

    That explains a lot.

    Soma

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Yes, they don't behave the same on all platforms. One of the likely culprits is endianess. So I would like to mark as non-portable.
    But obviously you don't agree. But we can't all agree.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Quote Originally Posted by Elysia View Post
    Plus binary files aren't portable.
    To be nitpicky, they are portable, just not across ports Each port itself should be fine as long as you don't communicate between different ports via files.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Good point. Perhaps we should call it semi-portable? Portable under certain conditions, that is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tough problem with function pointers
    By Montejo in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 12:17 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21