Last active
October 3, 2016 02:28
-
-
Save liladas/773d61cce219ffa1cc4d2c1f19629af1 to your computer and use it in GitHub Desktop.
read file example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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