Results 1 to 9 of 9

Thread: C++ prog: copy byte array

  1. #1
    Member
    Join Date
    Sep 2007
    Posts
    51

    Lightbulb C++ prog: copy byte array

    hey Guys,

    here is my problem; i have read some bytes from a file to a char array and i want to copy the bytes exactly to another char array. i would be grateful to whoever can tell me the solution to this problem.

    as, using strcpy() or strncpy(), will only copy the bytes up to the first null byte and then stop. and to use write() i would need a "file descriptor" for the destination array, and i am not sure it is possible to create a file descriptor for a program variable?


    thanks in advance,

    - user0182

  2. #2
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    Pseudocode:

    Code:
    stringa;
    stringb;
    
    p=0;
    
    while p< length(stringa) {
     stringb[p]=stringa[p];
     p++;
    }
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  3. #3
    Member
    Join Date
    Sep 2007
    Posts
    51
    thanks SirDice, i'll give it a try.

    but, i'm more of an old-style C programmer, so i'm not familiar with strings, i tend to use char arrays. so, could you tell me does the length() function on strings work differently to the strlen() function on char arrays, or does it still just measure up to the first null byte. because if so, i still have the same problem?


    regards,

    - user0182

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    Try sizeof. Strlen would indeed give the string length upto the first NULL.

    NB the length command was part of my pseudocode. I used it to give you an idea, it's no way near anything syntactically correct
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  5. #5
    Member
    Join Date
    Sep 2007
    Posts
    51
    i'm not sure what you mean by use sizeof(), this will just give me the size of the array i.e.:

    Code:
            int x;
            char str_a[1024];
    
            strcpy(str_a,"blah blah");
            x=sizeof(str_a);                //would return 1024
    what i was looking for is; if anyone knows of a C function that will copy bytes from one char array to another char array, including null bytes? as i'm reading image files etc.

    i know that the write() function would be perfect, providing that one can create a "file descriptor" for the destination array! but, i'm not sure one can create a file descriptor for a variable? if i have to then i can just write the function myself.

    i just thought i would ask if a function already exists, so that i might learn something new about C programming. as i'm sure it probably does, as it would be a useful function. anyway, thanks for trying to help SirDice.


    - user0182
    Last edited by user0182; July 16th, 2008 at 09:48 AM.

  6. #6
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    Hmm... Was searching around for you.. It seems a C++ string can handle null bytes.
    It's the old school C strings (char array) that uses a null byte to end the string.

    The write function is C not C++. IIRC in C++ it should be cout << .

    Also have a read here:
    http://www.velocityreviews.com/forum...to-char-x.html
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  7. #7
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Well basically, if you're wanting to copy the whole array then what SirDice told you is correct. You can do something like the following:

    Code:
    char a[50], b[50];
    int x=0;
    
    for(; x<sizeof(b); x++)
      a[x]=b[x];
    Once that has executed, a and b will have equivalent contents. Of course, you could also use memcpy:

    Code:
    char a[50], b[50];
    
    memcpy(a, b, sizeof(b));
    Have a look at http://www.cplusplus.com/reference/c...ng/memcpy.html

    ac

  8. #8
    Member
    Join Date
    Sep 2007
    Posts
    51
    thanks gothic_type, the memcpy() function was exactly what i was looking for.

    thanks for your help too SirDice, it is much appreciated, although when you said that "The write function is C not C++.", i thought that the write() function is just a system call and so applies equally to C and C++, as C++ is just an extension of C and not a new language. unlike, the printf() function which has been made pretty much redundant by "cout <<".


    regards,

    - user0182

  9. #9
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    IIRC write is used a little different in C++. But I have to admit it's been a while since I've done anything with C++.

    I'm mainly a perl monger and use C from time to time
    Oliver's Law:
    Experience is something you don't get until just after you need it.

Similar Threads

  1. Perl Tutorial, Part 1
    By ch4r in forum Other Tutorials Forum
    Replies: 0
    Last Post: May 30th, 2005, 09:23 PM
  2. Copy protection question
    By Dominaterx in forum Newbie Security Questions
    Replies: 3
    Last Post: November 6th, 2003, 01:54 PM
  3. Arrays Tutorial (+free source)
    By ntsa in forum Other Tutorials Forum
    Replies: 0
    Last Post: September 13th, 2002, 09:10 AM
  4. counting bits in byte array in Java
    By nancen in forum Newbie Security Questions
    Replies: 4
    Last Post: April 21st, 2002, 02:51 PM
  5. Black Wolf's Guide to Memory Resident Viruses.
    By ahmedmamuda in forum AntiVirus Discussions
    Replies: 2
    Last Post: March 20th, 2002, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •