![](/img/trans.png)
[英]Read each line from a file and split the line into a string and an array in C
[英]How to read each line into an integer array, from a file in C?
如何将在C程序中打开的文件的每一行读取为整数数组。 我不能使用fgets
和sscanf
因为事先不知道输入。 每行的长度和列数可以变化。
尝试了fscanf
, fgetc
和其他人,但他们似乎在检测换行时遇到了问题。 我最终一起读取整个文件,而不是读到不同的数组。
例如,该文件包含:
1 2 3 4 5
1 2 3
2 3 4
这应该存储在arr1[] = {1,2,3,4,5} , arr2[] = {1,2,3} , arr3[] = {2,3,4}
将字符输入每行缓冲区。 在行结束时(即,当你点击换行符\\n
),用strtok
或类似标记缓冲区。 将标记读入预先分配或可调整大小的数组或struct
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.