2012-10-30 192 views
1

所以我有一個文件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循環,然後它只會顯示最後數據行

+0

的零似乎來自注釋行,但我不知道爲什麼行重複 –

回答

2

的一個問題是,你總是輸出數字,即使你獲得了評論。另外,偷看和忽略的東西不應該是必要的。要檢查這些數字是否已成功讀取,只需讀取之後評估流作爲一個布爾值,就像我在這個例子做:

#include <fstream> 
#include <sstream> 
using namespace std; 

int main(int argc, char ** argv) 
{ 
    ifstream in(argv[1]); 
    ofstream out(argv[2]); 
    string line; 
    while(getline(in,line)) 
    { 
     if(line.empty() || line[0] == '#') continue; 
     double number, price, change; 
     stringstream ss(line); 
     if(ss >> number >> price >> change) 
      out << number << "\t" << price << "\t" << change << "\n"; 
    } 
    return 0; 
} 

這是,順便說一下,情況的例子,其中使用一些C功能會使事情變得更簡單,更漂亮:

#include <stdlib.h> 
#include <stdio.h> 
#include <iostream> 
using namespace std; 

int main(int argc, char ** argv) 
{ 
    string line; 
    int number; 
    double price, change; 
    while(getline(cin,line)) 
     if(sscanf(line.c_str(), "%d %lf %lf", &number, &price, &change)==3) 
      printf("%3d %8.2f %8.2f\n", number, price, change); 
    return 0; 
} 

本例使用標準輸入和輸出,而不是文件。這些被稱爲「標準」的原因是:它們非常靈活,可以非常簡單地重定向到/從文件和其他進程。但該程序與文件非常相似。

+0

非常感謝,這是非常有益的 –

+0

我剛剛得到一個問題請問這個第一implemenetation使用ostringstream和istringstream? –

+0

它沒有,所以如果這是關鍵的我想這是不是你所期待的。 ostringstream對於你正在做的事不是必需的,所以我沒有使用它。由於其標記屬性,istringstream很有用。我使用了一個普通的stringstream,但它可能已被替換爲istringstream而沒有任何更改。 – amaurea

1

您需要總是檢查您輸入成功了!此外,你很少需要訴諸奇怪的基於字符的閱讀技巧。例如,有一個操縱器可以跳過前導空白:std::ws。下面是一個對數值類型沒有太多假設的版本。如果某行的第一個值必須是一個整數,則可以使用int,而不是std::string,你甚至可以跳過註釋行的支票!您只需檢查讀取三個值是否成功。

#include <sstream> 
#include <fstream> 
#include <string> 

int main() 
{ 
    std::ifstream  in("input.txt"); 
    std::ofstream  out("output.txt"); 
    std::istringstream lin; 
    std::string  tmp0, tmp1, tmp2; 
    for (std::string line; std::getline(in, line);) { 
     lin.clear(); 
     lin.str(line); 
     if ((lin >> std::ws).peek() != '#' 
      && lin >> tmp0 >> tmp1 >> tmp2) { 
      out << tmp0 << '\t' << tmp1 << '\t' << tmp2 << '\n'; 
     } 
    } 
}