C/read file into char*

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<source lang="c"> #define CHUNK_SIZE 128 int readFile(char *filename, char **data) { FILE *f; char *c; /* contents*/ void *p; int l; - length: int r; /* amoun...")
 
m
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
This could be considerably improved by using <code>fseek()</code> and <code>ftell()</code>. However that approach won't work with streams, only ''static'' files.
 +
 
<source lang="c">
 
<source lang="c">
 
#define CHUNK_SIZE 128
 
#define CHUNK_SIZE 128
 
int readFile(char *filename, char **data) {
 
int readFile(char *filename, char **data) {
  FILE *f;
+
FILE *f;
+
 
  char *c; /* contents*/
+
char *c; /* contents */
  void *p;
+
void *p;
+
 
  int l; /* length */
+
int l; /* length */
  int r; /* amount read */
+
int r; /* amount read */
  int s; /* memory size */
+
int s; /* memory size */
+
 
  if ((f = fopen(filename,"r")) == NULL) {
+
if (!filename || !data) return 3;
    perror("fopen()");
+
 
 +
if ((f = fopen(filename,"r")) == NULL) {
 +
perror("fopen()");
 
return 1;
 
return 1;
  }
+
}
+
 
  c = NULL;
+
c = NULL;
  l = 0;
+
l = 0;
  s = 0;
+
s = 0;
  while (!feof(f)) {
+
while (!feof(f)) {
    s += CHUNK_SIZE;
+
s += CHUNK_SIZE;
    if ((p = realloc(c, sizeof(char) * s)) == NULL) {
+
if ((p = realloc(c, sizeof(char) * s)) == NULL) {
      perror("realloc()");
+
perror("realloc()");
      goto die;
+
goto die;
    }
+
}
    c = p;
+
c = p;
    r = fread(&(c[l]), 1, s - l, f);
+
r = fread(&(c[l]), 1, s - l, f);
    if (!r) {
+
if (!r) {
      if (feof(f)) break;
+
if (feof(f)) break;
      /* else error */
+
/* else error */
      fprintf(stderr,"fread(): An error occured\n");
+
fprintf(stderr,"fread(): An error occured\n");
      goto die;
+
goto die;
    } else if (r < 0) {
+
} else if (r < 0) {
      perror("fread()");
+
perror("fread()");
      break;
+
break;
    }
+
}
    l += r;
+
l += r;
  }
+
}
+
 
  if ((p = realloc(c, sizeof(char) * (l + 1))) == NULL) {
+
if ((p = realloc(c, sizeof(char) * (l + 1))) == NULL) {
    perror("realloc()");
+
perror("realloc()");
  } else {
+
} else {
    c = p;
+
c = p;
  }
+
}
+
 
  c[l] = '\0';
+
c[l] = '\0';
+
 
 
fclose(f);
 
fclose(f);
+
 
 
*data = c;
 
*data = c;
 
return 0;
 
return 0;
 +
 
die:
 
die:
 
fclose(f);
 
fclose(f);

Latest revision as of 11:25, 1 October 2014

This could be considerably improved by using fseek() and ftell(). However that approach won't work with streams, only static files.

#define CHUNK_SIZE 128
int readFile(char *filename, char **data) {
	FILE *f;
 
	char *c; /* contents */
	void *p;
 
	int l; /* length */
	int r; /* amount read */
	int s; /* memory size */
 
	if (!filename || !data) return 3;
 
	if ((f = fopen(filename,"r")) == NULL) {
		perror("fopen()");
		return 1;
	}
 
	c = NULL;
	l = 0;
	s = 0;
	while (!feof(f)) {
		s += CHUNK_SIZE;
		if ((p = realloc(c, sizeof(char) * s)) == NULL) {
			perror("realloc()");
			goto die;
		}
		c = p;
		r = fread(&(c[l]), 1, s - l, f);
		if (!r) {
			if (feof(f)) break;
			/* else error */
			fprintf(stderr,"fread(): An error occured\n");
			goto die;
		} else if (r < 0) {
			perror("fread()");
			break;
		}
		l += r;
	}
 
	if ((p = realloc(c, sizeof(char) * (l + 1))) == NULL) {
		perror("realloc()");
	} else {
		c = p;
	}
 
	c[l] = '\0';
 
	fclose(f);
 
	*data = c;
	return 0;
 
die:
	fclose(f);
	if (c) free(c);
	return 2;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox