Thread: memcpy binary

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    memcpy binary

    memcpy is not supposed to check for any terminating null character in the source, so why does it stop at the terminating zero character?
    I want to copy a binary file.

    Code:
    int main()
    {
        char buf[] = "bbbbbbbbb";
        
        memcpy (buf, "$\0aaa", 5);
        cout << buf << endl;
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It doesn't. But using << to print a char pointer does.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Haha, awesome! Thanks!
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Indeed, had it not copied the \0, then you'd get "$bbbbbbbb"

    You've probably already got it by now, but just fyi, you could do this instead, to output the whole thing, nulls and all:
    Code:
    for (int i=0; i<sizeof(buf), ++i)
        cout << buf[i];
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you have a good reason for not using std::copy?
    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.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    @iMalc good idea, Ill know it next time.

    @Elysia
    Didnt know std::copy was working with C-style strings. Thanks.
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Ducky View Post
    Didnt know std::copy was working with C-style strings. Thanks.
    Everything with an iterator interface (that include pointers) are supported, by design.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. when should I use memcpy()?
    By ppdouble in forum C Programming
    Replies: 19
    Last Post: 06-01-2012, 01:52 AM
  2. memcpy
    By sharp_wan in forum C Programming
    Replies: 19
    Last Post: 10-03-2011, 04:16 PM
  3. memcpy()
    By Moni in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2006, 05:31 AM
  4. memcpy
    By mbooka in forum C Programming
    Replies: 10
    Last Post: 02-28-2006, 02:25 AM
  5. Help: About memcpy()
    By naruto in forum C Programming
    Replies: 41
    Last Post: 06-25-2004, 03:47 PM