Skip to content

Instantly share code, notes, and snippets.

@vext01
Created March 15, 2012 09:46
Show Gist options
  • Save vext01/2043332 to your computer and use it in GitHub Desktop.
Save vext01/2043332 to your computer and use it in GitHub Desktop.
fgetln test
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *
somefunc(FILE* file)
{
char *copy;
size_t size = 0;
char *line = NULL;
if (file == NULL) {
return NULL;
}
if ((line = fgetln(file, &size)) == NULL) {
if (ferror(file))
perror("fgetln");
return NULL;
}
if ((copy = malloc(size+1)) == NULL) {
perror("malloc");
return NULL;
}
memcpy(copy, line, size);
copy[size] = '\0';
return copy;
}
int
main(void)
{
FILE *f = NULL;
char *got;
if ((f = fopen("test.c", "r")) == NULL)
perror("fuck");
for (i = 1; i < 10; i ++) {
got = somefunc(f);
printf("i got '%s'\n", got);
free(got);
}
fclose(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment