2011-11-18 51 views
-3

我想用C++讀取下面的文件。在C++中讀取一個具有整數的文件

000001011100110 
100000010101100 
001001001001100 
110110000000011 
000000010110011 
011000110101110 
111010011011110 
011001010010000 

我已經知道rowscolumns多少有在文件中。我想閱讀每個integer並將其存儲在整數中的2-D matrix。 這裏的每個整數表示0是一個條目,而1是另一個條目。所以在上面的例子中有150's和1 s。

+1

這樣嗎?有什麼問題? –

+0

假設每個條目的長度只有一位數是否安全? – moshbear

+0

@moshbear:是的 – Avinash

回答

1
#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

using namespace std; 

const size_t NROWS = /* number of rows */; 
const size_t NCOLS = /* number of cols */; 

int main() { 
    vector<vector<int> > myvec (NROWS, vector<int>(NCOLS)); 
    ifstream myfile ("example.txt"); 
    if (myfile.is_open()) 
    { 
     string line; 
     for (size_t rowcount = 0; rowcount < NROWS && getline (myfile, line); 
     ++rowcount) 
    { 
     size_t colcount = 0; 
     for (string::const_iterator it = line.begin(); 
      it != line.end() && colcount < NCOLS; ++it, ++colcount) 
     { 
      myvec[rowcount][colcount] = *it - '0'; 
     } 
    } 
     myfile.close(); 
    } 

    else 
    cerr << "Unable to open file" << endl; 

    return 0; 
} 

一個約std::vector<T>整齊的事情之一是,構造(size_t, T& default_value = T())都預先分配和轉讓。 因此,使用它創建多維數組非常簡單 - 只需根據需要嵌套向量,併爲第二個參數嵌套(n-1)級別。

+0

如果'myfile.good()'返回true,這並不意味着讀取會成功。這裏有一個解釋:http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong/5605159#5605159(這個問題提到'eof()',但是相同問題適用)。儘管如此,這個特殊的例子不會失敗。 –

1
int main() { 
    string line; 
    ifstream myfile ("example.txt"); 
    if (myfile.is_open()) 
    { 
    while (getline (myfile,line)) 
    { 
     cout << line << endl; 
     int lineNumber; 
     stringstream(line) >> lineNumber; 
     //Do smith with this line read amount of digits 
     int tempNumber = lineNumber; 
     num_digits = 0; 
     //get number of digits 
     while(tempNumber > 0) { 
     num_digits++; 
     tempNumber/=10; 
     } 
     //print digits 
     std::stringstream tmp_stream; 
     tmp_stream << tempNumber; 
     for (i = 0; i < tmp_stream.str().size(); i++) 
     { 
      std::cout << "Digit [" << i << "] is: " << tmp_stream.str().at(i) << std::endl; 
     } 
    } 
    myfile.close(); 
    } 

    else cout << "Unable to open file"; 

    return 0; 
} 

//獲取數字 數是一些額外的,也許你會喜歡得到數字的更好的辦法,所以這可能是有用的

正是你的目的只是這需要:

if (myfile.is_open()){ 
    int line = 0; 
    while (getline (myfile,line)) 
     { 
      cout << line << endl; 
      for (i = 0; i < line.size(); i++) 
      { 
       std::cout << "Digit [" << i << "] is: " << line.at(i) << std::endl; 
       int lineDigitNumber; 
       stringstream(line) >> lineDigitNumber; 
      } 
    line++; 

    //Fill array with line and i 
    } 
    myfile.close(); 
} 
+0

這裏的每個整數表示0是一個條目,1是另一個條目。所以在上面的例子中有15個0和1個。 – Avinash

+0

@Avinash我沒有得到它..對不起,再來 – 2011-11-18 07:55:14

+0

000001011100110不是一個數是不同的號碼0和1 – Avinash

0

這裏是我的 「演習」 了quick'n'dirty矩陣類的實現;)

#include <iostream> 
#include <fstream> 
#include <stdexcept> 
#include <string> 

class CMatrix { 

public: 

    CMatrix(unsigned int r, unsigned int c) 
    : rows(r), cols(c), a(new int [r*c]) 
    {} 

    // copy constructor and copy assignment operator come here 

    ~CMatrix() 
    { 
     delete [] a; 
    } 

    int& at(unsigned int r, unsigned int c) 
    { 
     if (r >= rows || c >= cols) throw std::out_of_range("Out of range access"); 
     return a[r*cols + c]; 
    } 

private: 

    unsigned int rows; 
    unsigned int cols; 
    int* a; 

}; 


int main() 
{ 
    // assuming the sample file OP provided 
    const unsigned int n_rows = 8; 
    const unsigned int n_cols = 15; 

    std::ifstream myfile("in.txt"); 

    if (!myfile.is_open()) { 
     // report error 
    } else { 

     CMatrix matrix(n_rows, n_cols); 
     std::string line; 
     unsigned int m = 0; 
     unsigned int n = 0; 

     while (getline(myfile, line)) 
     { 
      n = 0; 
      for (char& c : line) { 
       matrix.at(m,n) = c == '0' ? 0 : 1; 
       ++n; 
      } 
      ++m; 
     } 
    } 
}