memcpy

This is a discussion on memcpy within the C Programming forums, part of the General Programming Boards category; Regarding the fucntion below: Code: void * memcpy ( void * destination, const void * source, size_t num ); for ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    memcpy

    Regarding the fucntion below:
    Code:
    void * memcpy ( void * destination, const void * source, size_t num );
    for the source, can I use other than string to be copied? As example JPEG file? or TEXT file? or maybe any type of file?

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    A void pointer means that the function doesn't care what data type is pointed to.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sharp_wan View Post
    Regarding the fucntion below:
    Code:
    void * memcpy ( void * destination, const void * source, size_t num );
    for the source, can I use other than string to be copied? As example JPEG file? or TEXT file? or maybe any type of file?
    Not files... memory data... and yes memcpy will copy *anything*.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    I see. So, is there any suggestion of how i can read any file from the computer and put it into variable or array? which one the best? My idea now is to use fread function and then put this into a array then memcpy this array. Is this ok or there is better way of doing it? Suggestions?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are all kinds of ways to read files. You can read a byte at a time, a block at a time, as text, as binary. Just pick one.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, I would certainly start by looking up the functions you're going to need...fopen(), fread(), memcpy(), malloc(), fclose(), free() etc... to understand how each one works. The full descriptions of each should be in the C Library documentation that accompanies your compiler and it's libraries.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You don't need memcpy() to copy files, read in the file in memory then write that same memory back to another file.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Subsonics View Post
    You don't need memcpy() to copy files, read in the file in memory then write that same memory back to another file.
    I knew that ... but if the OP actually learns to use documentation, not only will he discover that, it will help him in other ways...

    In all truth you don't even need to open the file to copy it...
    On windows just use the Windows CopyFile() (or it's equivalent on whatever OS you're running).
    Last edited by CommonTater; 10-02-2011 at 08:28 PM.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    I have read the documentation but i still need some example, such as,

    let say i want to memcpy the JPEG picture in My Documents, and i want to put the JPEG into a buffer[200], what should i put in the argument? Refer below:

    Code:
    /* memcpy example */ 
    
    #include <stdio.h> 
    #include <string.h>  
    
    int main ()
     {
       FILE* pFile
    
       //how the program wanna fine the file ??
    
       pFile = fread(what should I put here??);
       char buffer[200];
       memcpy (buffer[200], what should I put here?? ,200); 
    
       return 0; 
    }

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    memcpy is for moving data from one memory buffer to another. Since your data is not in a memory buffer, memcpy won't help you.

    fread will:
    fread

    Declaration:

    size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    Reads data from the given stream into the array pointed to by ptr. It reads nmemb number of elements of size size. The total number of bytes read is (size*nmemb).
    On success the number of elements read is returned. On error or end-of-file the total number of elements successfully read (which may be zero) is returned.
    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.

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    ah, thats explain why memcpy not a good choice for this task...hmm...so fread should be able to read a JPEG file isn't it? How to read it though?

    Code:
    FILE* pFile;
    char * buffer;
    long lSize; 
    size_t result; 
    
    result = fread( buffer , what should I put here?? , lSize, pFile );

  12. #12
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by sharp_wan View Post
    ah, thats explain why memcpy not a good choice for this task...hmm...so fread should be able to read a JPEG file isn't it? How to read it though?

    Code:
    FILE* pFile;
    char * buffer;
    long lSize; 
    size_t result; 
    
    result = fread( buffer , what should I put here?? , lSize, pFile );
    Have a look at this link, there is an example of reading a complete file there.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sharp_wan View Post
    I have read the documentation but i still need some example, such as,

    let say i want to memcpy the JPEG picture in My Documents, and i want to put the JPEG into a buffer[200], what should i put in the argument? Refer below:
    Welll... the smart thing to do here is to look the functions you are using up in your C library documentation... yep, that means Read The Help File for your compiler.

    Honestly... more than 2/3 of the questions asked here could be answered in a few seconds by either opening a help file or doing a quick google search for the function in question....

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    Welll... the smart thing to do here is to look the functions you are using up in your C library documentation... yep, that means Read The Help File for your compiler.

    Honestly... more than 2/3 of the questions asked here could be answered in a few seconds by either opening a help file or doing a quick google search for the function in question....
    I know what you mean, believe me. i've read the documentation and stuffs. but my question is how to get the JPEG file or any file without using fopen. I know fopen has command "a" to open the already existed file, but is there any way to open the file only when the program asked? like scanf or something, instead putting string we can put the file location? is the anything like that? thats my question.

    sorry if the previous questions is not clear.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    you mean something like...
    Code:
    char FileNameStr[512];
    
    // ask user for filename here...
    
    fopen(FileNameStr,"r");
    Gee... I dunno... maybe you should try it and see what happens....

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

Similar Threads

  1. memcpy not doing its job
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 11-11-2010, 12:17 PM
  2. using memcpy?
    By nyekknyakk in forum C Programming
    Replies: 1
    Last Post: 08-19-2010, 08:47 PM
  3. memcpy
    By m37h0d in forum C++ Programming
    Replies: 28
    Last Post: 04-12-2008, 10:10 PM
  4. memcpy()
    By Moni in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2006, 06:31 AM
  5. Help: About memcpy()
    By naruto in forum C Programming
    Replies: 41
    Last Post: 06-25-2004, 04:47 PM

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