2012-08-14 68 views
1

如何從輸入中刪除空格?它是一個整數數組。 輸入是這樣的:如何忽略輸入文件中的空格?

5 6 2 9 
3 1 11 4 

我將不得不把它作爲一個整數。然後使用冒泡排序進行排序。問題是我收到輸出,如這些:9 8 7 4 2 1 -858993460 -858993460 -858993460 -858993460

class inout 
{ 
public : 
    int i[10]; 

    inout() 
    { 

    } 

    void read() 
    { 
     ifstream inClientFile("input.txt", ios::in); 
     if (!inClientFile) 
      { 
      cerr << "File could not be opened" << endl; 
      exit(1); 
      } 
     int n=0; 

     while (inClientFile >> i[n]) 
     { 

      n++; 
     } 
     cout << " Data read complete" << endl; 
    } 

    void write() 
    { 
     ofstream outClientFile("output.txt", ios::out); 

     if (!outClientFile) { 
      cerr << "File could not be opened" << endl; 
      exit(1); 

     } 


     int n=0; 
     for (int l=0 ; l < 10 ; l++) 
     { 
      outClientFile << i[l] << " " ; 

     } 
     cout << " Data Write complete" << endl; 
    } 

    void sortData(int asc) 
    { 
     int pos=0; 
     int temp; 
     temp = i[0]; 

     // asending 

     if (asc == 1) 
     { 
     for (int p = 0; p < 10 ; p ++) 
     { 

     int pos=p; 
     int temp2,temp = i[p]; 
      for (int j = p+1 ; j < 10 ; j ++) 
      { 
       if (pos == p) 
       { 
        if (i[p] > i[j]) 
        { 
         pos = j; 
         temp = i[j]; 
        } 
       } 
       else { 
        if (i[pos] > i[j]) 
        { 
         pos = j; 
         temp = i[j]; 
        } 
       } 
      } 

      temp2 = i[pos]; 
      i[pos] = i[p]; 
      i[p] = temp2; 
     } 


    } 
    else 
    { 
     for (int p = 0; p < 10 ; p ++) 
     { 

     int pos=p; 
     int temp2,temp = i[p]; 
      for (int j = p+1 ; j < 10 ; j ++) 
      { 
       if (pos == p) 
       { 
        if (i[p] < i[j]) 
        { 
         pos = j; 
         temp = i[j]; 
        } 
       } 
       else { 
        if (i[pos] < i[j]) 
        { 
         pos = j; 
         temp = i[j]; 
        } 
       } 
      } 

      temp2 = i[pos]; 
      i[pos] = i[p]; 
      i[p] = temp2; 
     } 


    } 
    } 

}; 

int main() 
{ 
    int d; 


    inout x; 
    x.read(); 
    cout<<"Press 1 to sort into ascending order"<<endl<<"Press any other key to sort into descending order"<<endl; 
    cin>>d; 
    x.sortData(d); 
    x.write(); 

} 
+0

應該避免 「幻數」 等在'對(INT p = 0時,P <10;的p ++)十'。 – ChiefTwoPencils 2012-08-14 00:42:57

+0

如何獲得一個字符串等價並做String.Replace(「」,「」);然後轉換爲整數使用string = int.Parse(string); – user959631 2012-08-14 00:45:56

回答

0

您應該記錄多少輸入號碼inout::read()實際上讀的,而不是總是排序和打印整個數組。您的示例輸入中有8個數字,但您的代碼總是排序並打印10個數字。

+0

嗯,我怎樣才能得到數組的大小? – 2012-08-14 00:49:29

+0

@ user1596763,最簡單的方法是使用'std :: vector '來保存文件內容。然後使用'std :: vector :: size()'來獲得大小。您還可以使用構造函數的範圍形式和'std :: istream_iterator'來初始化文件內容。 – chris 2012-08-14 00:57:28

+0

@KnightFall您可以將'inout :: read()'中計算的'n'存儲爲'class inout'的新成員變量,以便在'sortData()'和'write()'中獲得正確的元素數量。 – timrau 2012-08-14 12:10:26

0

問題不在於空格。流提取器將空白(包括多個空格,換行符,製表符)視爲分隔符。

輸入循環仔細計算它讀取的值的數量,但代碼的其餘部分忽略該計數。您的輸入示例有8個元素,但代碼是針對10個元素進行硬編碼的。數組i中的最後兩個元素具有無意義的值,這是麻煩的處方。

std::ifstream in("myfile"); 

std::stringstream buffer; 
buffer << in.rdbuf(); 

std::string contents(buffer.str()); 

然後使用普通的string操作技巧,以獲得整數:

0

可以使用ifstream的的rdbuf()方法來讀取整個文件的內容轉換爲字符串。 是這樣的:

istringstream iss(contents); 
int x=0; 
do{ 
x=0; 
iss>>x; 
}while(iss);