简体   繁体   English

如何从C中的文件中读取每一行到一个整数数组?

[英]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. 如何将在C程序中打开的文件的每一行读取为整数数组。 I cannot use fgets and sscanf as the input is not known beforehand. 我不能使用fgetssscanf因为事先不知道输入。 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 newline. 尝试了fscanffgetc和其他人,但他们似乎在检测换行时遇到了问题。 And I ended up reading the entire file together, instead of into different arrays. 我最终一起读取整个文件,而不是读到不同的数组。

eg, 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} 这应该存储在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 (ie, when you hit a newline character \\n ), tokenize the buffer with strtok or similar. 在行结束时(即,当你点击换行符\\n ),用strtok或类似标记缓冲区。 Read the tokens into a pre-allocated or resizable array or struct of your choice. 将标记读入预先分配或可调整大小的数组或struct中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从文件中读取每一行,然后在C中将该行拆分为一个字符串和一个数组 - Read each line from a file and split the line into a string and an array in C 无论如何,在C语言中,它们是逐行读取文件内容并将每个整数值(预行)分别存储到数组中的方式吗? - In C is their anyway way to read the contents of a file, line-by-line, and store each integer value (preline) into a array separately? 从文件中读取行并将每个单词存储到数组(C语言)中 - read line from file and store each word into an array (C language) 如何将每一行放入从 c 程序中的文件读取的数组中? - How do I put each line in an array which read from the file in c program? 如何读取 C 中每一行都有数字的文件? - How to read a file with numbers on each line in C? 如何从 .t​​xt 文件中读取整数并将它们存储在 C 中的整数类型数组中 - How to read integers from .txt file and store them in a integer type array in C 使用fgets从文件中读取行,并将每行与c中的strncmp进行比较 - read lines from file with fgets and compare each line with strncmp in c 如何读取文件并将每个readed行拆分为变量或数组 - how to read a file and split each readed line into variables or array 如何使用分隔符分割我从文件中读取的行并将它们存储在 C 的每个 int 变量中 - How to split line I read from file with delimiters and store them in each int variable in C 如果我打算将文件分成字符串,如何从文件中读取每一行? C - How to read each line from a file if I intend to split it up into strings? C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM