Jump to content

file_read block

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
vashek

vashek

    Newbie

  • Members
  • Pip
  • 2 posts
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


#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;

} 



Edited by vashek, 10 September 2009 - 09:23 AM.


#2
Guest_h4x_*

Guest_h4x_*
  • Guests

Quote

his is my first C code in my professional
rly?
omg, thats make me real pro here.

read about sys_mmap(), you map file to address space and access it like memory.

#3
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
What is the use of allocating memory of size 2 in function main()
char *file_content = (char *) malloc (2);

and reallocating it again in function get_file_content ()
file_content = (char *) realloc (file_content, sizeof(char) * file_size);

I don't find any use of allocating memory at the first place...

you can just define a char pointer in the function main()
char *file_content;

and allocate memory when needed.. (i.e) allocate memory only in the function get_file_content()
file_content = (char *) malloc (sizeof(char) * file_size);


#4
vashek

vashek

    Newbie

  • Members
  • Pip
  • 2 posts
Veda, Thanks for the info, I have updated my code snippet.

Is it okay to send an argument with NULL value in function call?

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
Depends on which argument. You can pass NULL to any function in any argument, but it might explode in your face if the code doesn't check for a null before using the argument.

Quote

read about sys_mmap(), you map file to address space and access it like memory.
@h4x: That's not portable. Perhaps there's a reason why vashek didn't use that.
sudo rm -rf /

#6
Guest_h4x_*

Guest_h4x_*
  • Guests

Quote

@h4x: That's not portable. Perhaps there's a reason why vashek didn't use that.
yes he didnt knew that function. and it is portable, every kernel has it, all 2.4 and 2.6 for sure.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
Windows doesn't. A professional would know that. Vashek never mentioned what operating system he/she's using, so you can't assume anything.
sudo rm -rf /

#8
Guest_h4x_*

Guest_h4x_*
  • Guests
Well i can assume linux. Windows is for noobs, and i dont want to insult him for no reason.
Somehow i feel not to help writing software for system of evil.
and yeah, windows doesnt have it. but there is MapViewOfFile, and you can write own mmap function.

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
By the way, vashek, you're assuming that the file you're reading in is ASCII text. If it's UTF16-encoded, or a binary file, you're going to get a much shorter string because there's bound to be a null byte before the end of the file. Just thought I'd throw that out there.
sudo rm -rf /