Last active
December 30, 2015 03:19
-
-
Save frank4565/7768789 to your computer and use it in GitHub Desktop.
Read a file line by line and put all numbers in one line to an array.
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 "stdlib.h" | |
#include "string.h" | |
/* Sample Input *** input_vectors.txt | |
0 1 | |
1 1 | |
0 1 | |
1 1 | |
0 1 | |
1 0 | |
0 0 | |
1 0 | |
0 0 | |
1 0 | |
*/ | |
int main(int argc, char *argv[]) | |
{ | |
char line[500]; | |
FILE *file = fopen(argv[1], "r"); | |
while (fgets(line, sizeof(line), file) != NULL) { | |
char* end = line; | |
int count = 0; | |
do { | |
strtol(end, &end, 10); | |
count++; | |
end++; | |
} while (strcmp(end, "\n")); | |
int *array = (int *)malloc(sizeof(int)*count); | |
end = line; | |
for (int i = 0; i < count; i++) { | |
array[i] = (int)strtol(end, &end, 10); | |
end++; | |
} while (strcmp(end, "\n")); | |
// do something with the array | |
printf("%d\n", array[1]); | |
free(array); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment