怎么读取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;
}
[其他解释]
用getline函数不就ok了
[其他解释]
直接scanf就是了。
[其他解释]
char buf[1024];
scanf("%*[^ ]%*[ ]%s", str);
//把第一列数据丢弃就可以了
[其他解释]
不好意思啊,我没说清楚,这是一个股票数据,有16万个,用个数组肯定没法读
[其他解释]
scanf 不是C语言的输入吗,求具体怎么用?
[其他解释]
先用fget把第一行的字符读掉 然后想干嘛干嘛
[其他解释]
看错 是第二列不是第二行 那得加判断了
[其他解释]
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{
int temp, data[1024];//定义变量
fstream inputFile;//定义文件
inputFile.open("股票.txt", ios::in);//打开文件
if(!inputFile)
{
cout <<"1" << "无法打开" << endl; cin.get();
}//状态
for(int i=0;i<100;i++)
{
inputFile>>temp>>data[i];
cout << data[i] << endl;
}
inputFile.close();
}
我写的这个,只有第一行正确了,其他行全是负数,我那TXT每行的长度不一样
[其他解释]
000001 20080717 14.76 14.83 14.16 14.3 131173.9 31 0 3 4
000001 20080718 14.65 14.87 14.2 14.87 213512.0 33 +- 3 4321 1 5 60 0 69 0 0 0 0 0 0 0 0
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 不知道要多麻烦
[其他解释]
每列都读,然后用你需要的那列即可,其它的忽略。