2012-08-14 65 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);