Thread: Copy any type of file to a memory buffer?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    Copy any type of file to a memory buffer?

    Hello, is there any way to copy any type of binary/text file to a memory buffer, and then use the buffer to make a copy of that file?
    Code:
    char* readFileToBuffer(const char* fname, const char* opts) {
        FILE* fp = fopen(fname, opts);
        if ( !fp ) return 0x00;
        fseek(fp, 0, SEEK_END);
        long fsize = ftell(fp);
        printf("readFileToBuffer(): %d\n", fsize);
        char *buff = (char*)malloc(sizeof(char)* fsize+1);
        fseek(fp, 0, SEEK_SET);
        char* begin = buff;
        fread((char*)buff,  fsize, 1,fp);
        fclose(fp);
        return buff;
    }
    This, actually can`t be used to copy to a new file from the return buffer.
    Last edited by heatblazer; 01-13-2015 at 01:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mixed-type contiguous memory buffer
    By Prandtl in forum C Programming
    Replies: 2
    Last Post: 03-02-2014, 01:10 AM
  2. Replies: 5
    Last Post: 12-20-2012, 07:10 PM
  3. Copy HWND into shared memory buffer
    By Opariti in forum Windows Programming
    Replies: 2
    Last Post: 12-26-2009, 01:08 PM
  4. Copy .dat file to dynamic memory...
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 05-28-2007, 04:36 PM
  5. copy int to buffer??
    By Gugge in forum C Programming
    Replies: 4
    Last Post: 03-19-2002, 12:17 PM