从文本文件读取数据 [英] read data from text file

查看:138
本文介绍了从文本文件读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  2      1     3       6    0    9    0
         2     9       5    0    0    8
         3    10       0    6    0    6
  3      1     1       0    4    0    8
         2     1       7    0    0    8
         3     5       0    4    0    5
  4      1     3      10    0    0    7
         2     5       7    0    2    0
         3     8       6    0    0    7
  5      1     4       0    9    8    0
         2     6       2    0    0    7
         3    10       0    5    0    5
  6      1     2       2    0    8    0

我有很多文本文件。格式与上面的一样。我希望将每列数据存储到不同的数组,例如 col01 [5] = {2,3,4,5,6} (对应于第一列)。我如何做到这一点? col02 [15] = {1,2,3 ......} (对应于第2列数据)。

I have many text files. The format is like the above one. I hope to store each column data to different array, e.g., col01[5] ={2,3,4,5,6}(corresponding to the 1st column). How can I do this? col02[15] ={1,2,3......}(corresponding to the 2nd column data).

第一列中的数字不是固定的,位置也是随机的。例如,第一列中的数字随机位于某些行中。列号为固定。它可以采用以下格式:

The number in the first column is not fixed and the position is also random. For example, the numbers in the first column are randomly located in some lines. The column number is fixed. It may be in the following format:

  2      1     3       6    0    9    0
  2      2     9       5    0    0    8
         3    10       0    6    0    6
  3      1     1       0    4    0    8
         2     1       7    0    0    8
  5      3     5       0    4    0    5
  4      1     3      10    0    0    7
         2     5       7    0    2    0
         3     8       6    0    0    7
  5      1     4       0    9    8    0
         2     6       2    0    0    7
         3    10       0    5    0    5
  6      1     2       2    0    8    0

我试图使用 istringstream getline 但它太复杂了。感谢

I tried to use istringstream and getline but it is too complicated. Thanks

推荐答案

更简单更高效的方法是按字符扫描文件,即增加i每个值。
if(i ==)//如果字符是SPACE然后什么也不做
/ \ / \ if(i == 10)//如果字符是ascii ),即ENTER,然后切换到col01
/ \ / \ else继续在col01中存储DIGITS,然后转到col02直到col07。

The simpler and more efficient way would be to scan the file character by character, i.e increment "i" aand compare for each value. if(i==" ") // if the character is " " SPACE then do nothing /\/\ if(i==10) // if the character is ascii(10) i.e ENTER then switch to col01 /\/\ else go on storing the DIGITS in col01, then col02 on and on till col07.

是你的问题的解决方案的抽象。
希望它有帮助。
如果现在不让我,我很乐意再次帮助。

This is the abstract of your problem's solution. Hope it helps. If it doesn't let me now, I'll be glad to help again.

这篇关于从文本文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆