Thread: Load file from disk to memory

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36

    Load file from disk to memory

    I'm trying to program very simple web server and I'm stuck on how to fill buffer with file content.
    Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
        
        char * buffer = malloc(512);
        
        FILE *fp;
        fp = fopen("./index","r");
        char chr;
        char *reset;
        printf("Buffer address: %x\n", buffer);
        
        reset = buffer;
        
        while( (chr = getc(fp)) != EOF){
            *buffer = chr;
            buffer++;
            printf("%c", *buffer); 
        }
        
        printf("*Buffer address after fill: %x\n", buffer);
    
        buffer = reset;
        
        printf("*Buffer address after reset: %x\n", buffer);
        
        /* print content of buffer */
        while(*buffer != EOF){
            printf("%c", *buffer);
            buffer++;
        }
           
        
        return 0;
    }
    So I get trash on the output. Looks like nothing has been stored in buffer...
    Maybe I'm doing it completely wrong.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    EOF is never stored in your buffer; so I don't see how your last while loop will stop.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36
    Oh yes...how I missed that one...
    I added:
    Code:
    *buffer = EOF;
    ...right after buffer fill function(while...loop)
    And now there is no output at all.

    EDIT:
    One more thing. Is it better to store file to heap with malloc( ) or to stack using arrays?
    Last edited by dotunix; 10-21-2009 at 07:47 AM.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    It is better to read blocks of data from a file. You will be VERY slow if you read large files one byte at a time. Look up read(). Also, why buffer the whole file if you plan to just print it to stdout? Just dump each block to stdout as you read() [just be sure to add a '\0' to the end of your buffer].

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36
    Quote Originally Posted by Kennedy View Post
    It is better to read blocks of data from a file. You will be VERY slow if you read large files one byte at a time. Look up read(). Also, why buffer the whole file if you plan to just print it to stdout? Just dump each block to stdout as you read() [just be sure to add a '\0' to the end of your buffer].
    Yeah, you're right...too many reads and writes.
    I'm currently using stdout as a test, but it will be replaced with socket file descriptor later in child process.
    I'll rewrite the code and see if works.

    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Using a file like Virtual Memory
    By firetheGlazer in forum C Programming
    Replies: 3
    Last Post: 07-01-2008, 04:44 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM