2009-08-09 71 views
-1

我有一個成員函數,要求用戶輸入文件名,然後從文件輸入並在不同的函數上使用它。每個函數都帶有字符或字符。然而它並不完全執行這些功能。我認爲它與從文件中挑選數據然後使它們變成字符有關?是否有可能使用sstream從文件中分離元素,然後將這些元素用作字符?帶字符的流問題

void My_Function::file() 
{ 
    fstream data;//file input 
    char filename[80]; 
    string line; 
    int first; 
    char sec, third, fourth; 

    cout<<"Enter file name: \n"; 
    cin>>fileName; 
    data.open(fileName); 

    while(getline(data,line)) 
    { 
     stringstream str(line); 
     istringstream ins; 
     ins.str(line);//get line 
     str >> first >> sec >> third >> fourth; 

     switch(first) 
     { 
      case 1: 
        add(sec); 
        break; 
      case 2: 
        delete_item(sec, third); 
        break; 
      case 3: 
        print_everything(sec, third); 
        break; 
      case 4: 
        makenew(sec, third); 
        break; 
      case 5: 
        find(sec, third, fourth); 
        break; 
      case 0: 
        break; 
     } 
    } 
} 
+0

考慮制定'fileName'一個字符串,以防止緩衝區溢出。 – outis 2009-08-09 06:46:43

回答

0

書面您的代碼無法編譯,但固定「文件名」 /「文件名」的問題後,在我看來,這個特殊的部分做的事情是應該做的。你應該考慮嘗試澄清你的問題,並解釋你期望的代碼做什麼以及它與實際做法有什麼不同。 「不完全執行這些功能」的含義確實有點不明確!

爲了測試這樣的事情,它有時是有益的像

delete_item(sec, third); 

線改爲

cout << "delete_item(" << sec << ", " << third << ")\n"; 
+0

我試過了,文件中的所有輸入都是它應該在的位置。這會改變它的記憶類型嗎? stringstream str(line); istringstream; ins.str(line); //獲得行 str >> first >> sec >> third >> fourth; – 2009-08-09 15:36:02

0

您可能需要更改

stringstream str(line); 
istringstream ins; 
ins.str(line);//get line 
str >> first >> sec >> third >> fourth; 

istringstream ins(line); 
ins >> first >> sec >> third >> fourth; 
+0

我試過了,它做的是一樣的。整數工程,但char的似乎沒有做那裏的工作。 – 2009-08-09 15:37:03

1

我認爲你的問題是與數據。對於你的代碼工作,它應該是如下形式:

0 1 2 3 
1 1 2 3 
2 1 2 3 
3 1 2 3 
4 1 2 3 
5 1 2 3 

發佈您的數據樣本,供我們檢查。

此代碼按預期工作與數據(你的代碼在主與COUTS):

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

int main() 
{ 
    fstream data;//file input 
    char fileName[80]; 
    string line; 
    int first; 
    char sec, third, fourth; 

    cout<<"Enter file name: \n"; 
    cin>>fileName; 
    data.open(fileName); 

    while(getline(data,line)) 
    { 
     stringstream str(line); 
     istringstream ins; 
     ins.str(line);//get line 
     str >> first >> sec >> third >> fourth; 

     cout << "line = " << str.str() << endl; 

     switch(first) 
     { 
      case 1: 
        cout << "add(sec); " << sec << endl; 
        break; 
      case 2: 
        cout << "delete_item(sec, third); " << sec << ", " << third << endl; 
        break; 
      case 3: 
        cout << "print_everything(sec, third); " << sec << ", " << third << endl; 
        break; 
      case 4: 
        cout << "makenew(sec, third);" << sec << ", " << third << endl; 
        break; 
      case 5: 
        cout << "find(sec, third, fourth); " << sec << ", " << third << ", " << fourth << endl; 
        break; 
      case 0: 
        cout << "0" << endl; 
        break; 
     } 
    } 
} 

與輸出:

Enter file name: 
toto 
line = 0 1 2 3 
0 
line = 1 1 2 3 
add(sec); 1 
line = 2 1 2 3 
delete_item(sec, third); 1, 2 
line = 3 1 2 3 
print_everything(sec, third); 1, 2 
line = 4 1 2 3 
makenew(sec, third);1, 2 
line = 5 1 2 3 
find(sec, third, fourth); 1, 2, 3 
line = 
find(sec, third, fourth); 1, 2, 3 
相關問題