Archived

This topic is now archived and is closed to further replies.

-vic-

Can i do this with memcpy?

Recommended Posts

-vic-    112
Can i do this with memcpy? [cpp] FILE *fp; char buf[256]; fp = fopen("file", "rb"); memcpy(buf, fp, 256); [/cpp] Victor.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I''m beginning to see why the Unix elite took to their mantra of RTFM

Share this post


Link to post
Share on other sites
BriTeg    168
Sure, you can do it. It won''t do what you''re probably wanting, though. It''s copying the first 256 bytes of a FILE structure (not the file itself!) into buff. A FILE struct is only 32 bytes (in Visual C++ 6.0 on Windows, anyway), so the rest of your 256 bytes will just be garbage.

If you want the first 256 bytes of a file, try:


  
FILE *fp;
char buf[256];

fp = fopen("file", "rb");
if(fp)
{
fread(buff, 256, 1, fp);
}

Share this post


Link to post
Share on other sites
-vic-    112
quote:
Original post by Anonymous Poster
I''m beginning to see why the Unix elite took to their mantra of RTFM


If you are suggesting i should''ve read the manual for memcpy then i should tell you that i read the manpages for memcpy, but it says nothing about not working with reading files. A file could be easily copied to memory. Dumbass.

Thanks for the reply, BriTeg. I know i can do fread, but i was just wondering if it could be done with memcpy.

Victor.

Share this post


Link to post
Share on other sites
MauMan    344
If you want to read/write files like memory in unix look at the man pages for mmap. Under windows look at CreateFileMapping.

[edited by - MauMan on May 27, 2003 4:42:31 PM]

Share this post


Link to post
Share on other sites
-vic-    112
quote:
Original post by MauMan
If you want to read/write files like memory in unix look at the man pages for mmap. Under windows look at CreateFileMapping.

[edited by - MauMan on May 27, 2003 4:42:31 PM]


Thanks!
Victor.

Share this post


Link to post
Share on other sites