Skip to content

Instantly share code, notes, and snippets.

@liladas
Last active October 3, 2016 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liladas/773d61cce219ffa1cc4d2c1f19629af1 to your computer and use it in GitHub Desktop.
Save liladas/773d61cce219ffa1cc4d2c1f19629af1 to your computer and use it in GitHub Desktop.
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