Skip to content

Instantly share code, notes, and snippets.

@liladas liladas/readfile.c
Last active Oct 3, 2016

Embed
What would you like to do?
read file example
#include <stdio.h>
#include <string.h>
int main(void) {
puts("Reading File");
FILE *fp; // file pointer to data file
fp = fopen ("data.txt", "r");
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("data.txt", "r");
while ((read = getline(&line, &len, fp)) != -1) {
printf("%s", line);
/// getline allocates memory for you using 'realloc' internally
// notice i can pass it a NULL char *
//
// now parse the comma delimated line w/ strtok
char *token = strtok (line,",");
char * dest = NULL;
while (token != NULL) {
printf("Parsed %s\n", token);
token = strtok (NULL, ",");
}
}
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.