Regarding the fucntion below:
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?Code:void * memcpy ( void * destination, const void * source, size_t num );
Regarding the fucntion below:
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?Code:void * memcpy ( void * destination, const void * source, size_t num );
A void pointer means that the function doesn't care what data type is pointed to.
Code:while(!asleep) { sheep++; }
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?
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.
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.
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 07:28 PM.
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; }
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.
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.
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.
you mean something like...
Gee... I dunno... maybe you should try it and see what happens....Code:char FileNameStr[512]; // ask user for filename here... fopen(FileNameStr,"r");