简体   繁体   English

如何使用c / c ++语言从文件中读取1000列或更多列数据?

[英]How to read 1000 or more columns data from file using c/c++ language?

A data file with 10000 rows and 1000 columns. 具有10000行和1000列的数据文件。 I want to save a entire line to an array or each column to a variant. 我想将整行保存到数组,或者将每一列保存到变量。

There is a standard function fscanf in C. If use this function, I need write the format 1000 times. C中有一个标准函数fscanf 。如果使用此函数,则需要将格式写入1000次。

fscanf(pFile, "%f,%f,%f,%f,%f,%f,......", &a[0], &a[1],...,a[999]);

It is almost impossible like this when programming in C. But, I have no idea to implement it with C language. 用C进行编程时几乎是不可能的。但是,我不知道要用C语言来实现它。 Any suggestions or solutions? 有什么建议或解决方案吗?

And, how to read or extract some of columns data? 而且,如何读取或提取某些列数据?

Read the file line by line using fgets() into a suitably large buffer. 使用fgets()将文件逐行读取到适当大的缓冲区中。 Don't be afraid to use a buffer of 32 KB or something, just to be very sure all the fields fit. 不要害怕使用32 KB或类似大小的缓冲区,只是要确保所有字段都适合。

Then parse the line in a loop, perhaps using strtok() or just plain old strtod() . 然后,可以使用strtok()或仅使用普通的strtod()来循环分析该行。 Note that the latter returns a pointer to the first character that was not considered a number; 注意,后者返回一个指向第一个字符的指针,该字符不被视为数字。 this is where your parsing will continue for the next number. 这是您将继续解析下一个数字的地方。 Perhaps you need to add an inner loop to "eat" whitespace (or whatever separators you have). 也许您需要添加一个内部循环来“吃掉”空白(或任何您拥有的分隔符)。

您可以逐行读取文件,然后循环提取数字。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM