仅读取文本文件中的数字并存储在数组中.之后,读取第一行来存储在数组中

C++: Reading only numbers in text file and store in array. After, reading the first row to store in array too

本文关键字:存储 数组 读取 一行 之后 数字 取文本 文件      更新时间:2023-10-16

我有"Data.txt"的内容

Row 1: ACB MMM
Row 2: Date High    Low Open    Close   Volume  Adjusted Close  Date    High    Low Open    Close   Volume  Adjusted Close
Row 3: 40000    10  12  16  17  1500    17.2    40002   12  11  14  12  1200    12.2
Row 4: 40001    17.2    14  15  16  2500    18  40003   13.2    12  13  13  2300    13      

注意:第一行和第二行只能是字符串,每个字符串用制表符分隔。从第三行开始,只有数字,每个字符串用制表符分隔。

我想从第3行读取到存储在数组"data"和读取第1行存储在数组"symbol"。

谢谢你的帮助。

我的代码如下链接:http://www.mediafire.com/view/qfxtvj8sr022rff/Code.txt

使用std::getline逐行读取文件

#include <iostream>
#include <fstream>
#include <string>
. . .
ifstream file("Data.txt");
string line;
while (getline(file, line)) {
    . . .
}

使用std::stringstream从字符串

中读取
#include <sstream>
stringstream ss(line);
ss >> . . .;