怎么读取TXT第二列的数据呢
以空格为间隔,读取每行第二列的数据
[最优解释]
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<string> datas;
string line;
string data;
ifstream infile;
infile.close();
infile.clear();
infile.open("stock.txt");
if (infile.is_open())
{
while (getline(infile,line)) //每次读取一行
{
int i = 1;
istringstream ms(line); //把每行放入string 流中
while (ms >> data) //以空格为分割符读取字符串
{
if (i == 7) //当读到第列时打印出来和保存起来
{
datas.push_back(data);
cout << data <<endl;
}
i++;
}
}
}else
{
return EXIT_FAILURE;
}
system("pause");
return EXIT_SUCCESS;
}
000001 20080721 14.66 15.33 14.58 15.25 168314.9 13 -+ 3 4321 2 11 4 35 0 62 69 0 0 0 0 0 0 0
000001 20080722 15.26 15.54 15.19 15.29 85079.8 11 -o 3 4321 2 11 4 17 0 51 62 69 0 0 0 0 0 0
000001 20080723 15.45 15.71 15.06 15.15 112208.2 21 ++ 2 4321 1 11 3 55 0 45 51 62 69 0 0 0 0 0
000001 20080724 15.33 15.99 15.27 15.95 266657.30000000005 13 +- 4 4321 1 10 5 72 0 76 45 51 62 0 0 0 0 69
000001 20080725 15.65 16.42 15.53 16.17 290566.9000000001 13 oo 3 4321 1 01 4 51 0 56 76 45 51 0 0 0 0 64
000001 20080728 16.37 16.97 16.3 16.72 353765.1 12 +- 3 4321 2 11 5 53 0 67 56 76 45 0 0 0 0 66
给大家看看股票的格式 第七列该怎么读
[其他解释]
可以把数据导入数据库 然后再可以直接读列的数据 不然弄TXT 不知道要多麻烦
[其他解释]
每列都读,然后用你需要的那列即可,其它的忽略。