Can i do this with memcpy?

Started by
9 comments, last by -vic- 20 years, 11 months ago
Can i do this with memcpy? [cpp] FILE *fp; char buf[256]; fp = fopen("file", "rb"); memcpy(buf, fp, 256); [/cpp] Victor.
c[_]~~
Advertisement
no
I''m beginning to see why the Unix elite took to their mantra of RTFM
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);}  

Brianmiserere nostri Domine miserere nostri
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.
c[_]~~
He''s suggesting you read the manpages on fopen and file reading.
daerid@gmail.com
If the manual doesn''t say anything about something, it usually means that the topic is not related.

Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
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]
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
I know how to read/write files. I was just wondering if that memcpy would work.

Victor.
c[_]~~
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.

c[_]~~

This topic is closed to new replies.

Advertisement