Thread: common File_read block

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    common File_read block

    Hi All,

    I wanted to write a common function to retrieve file content. I do not know whether below code is upto the mark or not.

    any Suggestions/modifications/tips are greatly appreciated.

    Regards,
    Vashek

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
    Function Name: get_file_content
    Description: fills content of the file to given buffer.
    Parameters: file to be read(in), Mode(in), destination buffer(out)
    Return Type: pointer to content (SUCCESS), otherwise NULL
    */
    char* get_file_content (char *file_name, char *file_mode, char *file_content)
    {
    	FILE *fp;
    	long file_size;	
    	size_t bytes_read;
    	
    	//open file
    	if (!(fp = fopen (file_name, file_mode))) {
    		printf ("*** Unable to open file ***\n");	
    		return NULL;
    	}
    	
    	//get file size
    	fseek (fp, 0, SEEK_END);
    	file_size = ftell (fp);
    	rewind (fp);
    	printf("*** size = %ld ***\n", file_size);
    
    	//check memory
    	file_content = (char *) malloc (sizeof(char) * file_size+1);
    	if (file_content == NULL) {
    		printf("*** memory insufficient ***");
    		fclose (fp);
    		return NULL;
    	}
    
    	//get content
    	bytes_read = fread (file_content, sizeof(char), file_size, fp);	
    
    	//put EOS
    	file_content[bytes_read] = '\0';	
    
    	//clean	
    	fclose (fp);
    
    	//success
    	return file_content;
    }
    
    
    int main (int argC, char *argV[])
    {
    	int	status = -1;
    	char *file_content = NULL;
    	if ((file_content = get_file_content ("metadata.txt", "r", file_content)) == NULL){
    		status = 1;	
    		goto FREE_BLOCK;
    	}
    		
    	printf("\n#%s#\n", file_content);
    
    
    	FREE_BLOCK:
    		if(file_content)
    			free (file_content);
    
    	return 0;
    }
    Last edited by vashek; 09-10-2009 at 11:49 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think it is better to use stat() to get the file size, and that will also allow you to check to make sure the file is a regular file before you open it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Thanks MK27.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    file name and mode should be passed as const char*

    why do you need to pass file_content as a parameter?

    ftell is limited to 2Gb files

    do you plan to work with binary files as well?

    if yes - adding zero at the end of the buffer could cause problems when the file content will be processed by decoding routines for example

    has no meaning at all as indicator of buffer end - as the buffer could contain zeros before it

    and the content length should be returned as well.

    If you suppose to process only text files - why do you need to pass mode?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Quote Originally Posted by vart View Post
    file name and mode should be passed as const char*

    why do you need to pass file_content as a parameter?

    ftell is limited to 2Gb files

    do you plan to work with binary files as well?

    if yes - adding zero at the end of the buffer could cause problems when the file content will be processed by decoding routines for example

    has no meaning at all as indicator of buffer end - as the buffer could contain zeros before it

    and the content length should be returned as well.

    If you suppose to process only text files - why do you need to pass mode?
    good information... Thanks Vart.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. pointers
    By fanaonc in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 02:18 AM