Like Tree1Likes
  • 1 Post By manasij7479

memcpy binary

This is a discussion on memcpy binary within the C++ Programming forums, part of the General Programming Boards category; memcpy is not supposed to check for any terminating null character in the source, so why does it stop at ...

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

    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;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    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
    790
    Haha, awesome! Thanks!
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    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
    790
    @iMalc good idea, Ill know it next time.

    @Elysia
    Didnt know std::copy was working with C-style strings. Thanks.
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Ducky likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 01:25 AM
  5. Help: About memcpy()
    By naruto in forum C Programming
    Replies: 41
    Last Post: 06-25-2004, 03:47 PM

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