• Announcements

Archived

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

Can i do this with memcpy?

Recommended Posts

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

no

Share on other sites
I''m beginning to see why the Unix elite took to their mantra of RTFM

Share on other sites
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 on other sites
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 on other sites
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 on other sites
I know how to read/write files. I was just wondering if that memcpy would work.

Victor.

Share on other sites
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 on other sites
Cool, I was wondering if I could use memory mapped files under Linux.

• Forum Statistics

• Total Topics
627700
• Total Posts
2978690

• 20
• 14
• 12
• 10
• 12