所以我有一個文件data3.txt基本上是這樣的:sstream使用輸入和輸出文件
#file:data.txt
#data inputs
1 1234 +0.2 23.89 6.21
2 132 -0.03 3.22 0.1
3 32 0.00 31.50 4.76
而且我要採取的第一個3列使用stringtreams
寫入到一個新文件#include <cctype>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
string line;
float curr_price, change;
int stock_number;
ifstream fin("data3.txt");
istringstream iss;
ostringstream oss;
if(!fin){
cerr<<"Can't open a file";
}
ofstream outfile("data2.txt");
while (getline(fin,line)){
iss.clear();
iss.str(line);
iss>>stock_number>>curr_price>>change;
while(isspace(iss.peek()))
iss.ignore();
while(iss.str() == "#")
iss.ignore();
if(iss.str()==""){
break;
}
oss<<stock_number<<"\t"<<curr_price<<"\t"<<change<<"\n";
outfile<<oss.str();
}
}
但我我的輸出文件看起來壞壞:
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
1 1234 0.2
0 0 0
0 0 0
1 1234 0.2
2 132 -0.03
0 0 0
0 0 0
1 1234 0.2
2 132 -0.03
3 32 0
我沒有從那裏也從零,一來到想法第二,如果我把ofstream的出while循環,然後它只會顯示最後數據行
的零似乎來自注釋行,但我不知道爲什麼行重複 –