开发者

How to read each line into an integer array, from a file in C?

How do I read each line of a file opened in my C program to an integer array. I cannot use fgets and sscanf as the input is not known beforehand. The length of each row and the number of columns can vary.

Tried fscanf, fgetc, and others, but they seem to run into problems while detecting the newlin开发者_StackOverflow社区e. And I ended up reading the entire file together, instead of into different arrays.

e.g., the file contains:

1 2 3 4 5

1 2 3

2 3 4

This should be stored in arr1[] = {1,2,3,4,5} , arr2[] = {1,2,3} , arr3[] = {2,3,4}


Feed characters into a per-line buffer. Upon end-of-line (i.e., when you hit a newline character \n), tokenize the buffer with strtok or similar. Read the tokens into a pre-allocated or resizable array or struct of your choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜