[英]How do I read input from files?
我正在嘗試將文件中的輸入讀入數組。 我似乎已經完成了必要的工作,但是代碼卻無法正常工作。 請告訴我我要去哪里錯了。 這是我的代碼:
int pb[10][10];
int i,j,n;
string ip_filename = string("pro.txt");
ifstream fil1;
fil1.open(ip_filename.c_str());
// to store the probabilities of the nodes
for(i=0;i<num_rows;i++)
for(j=0;j<num_cols;j++)
fil1 >> pb[i][j];
fil1.close();
for(i=0;i<num_rows;i++)
{
for(j=0;j<num_cols;j++)
cout<<pb[i][j]<<" ";
cout<<endl;
}
文本文件與cpp文件位於同一目錄中。 打印輸出時,無論文件中的值如何,它僅打印0。
文件中的值存儲如下
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
之前在代碼中定義了num_rows
和num_cols
,它們的值均為4。
這段代碼的pro.txt格式對您來說非常適合我,如下所示:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int num_rows = 4;
int num_cols = 4;
int pb[10][10];
int i,j,n;
string ip_filename = string("pro.txt");
ifstream fil1;
fil1.open(ip_filename.c_str());
// to store the probabilities of the nodes
for(i=0;i<num_rows;i++)
for(j=0;j<num_cols;j++)
fil1 >> pb[i][j];
fil1.close();
for(i=0;i<num_rows;i++)
{
for(j=0;j<num_cols;j++)
cout<<pb[i][j]<<" ";
cout<<endl;
}
}
我的建議是確保pro.txt與.exe文件位於同一目錄中。 如果使用IDE生成此代碼,則該目錄可能與.cpp文件不同。
通常,執行此類操作的最簡單方法是將數據存儲在平面數組(甚至更好的是std::vector
)中,並使用簡單的算法按行和列訪問元素。 這使事情變得簡單得多。
一個包裝器可能看起來像這樣:
template<int ColumnCount>
class MyMatrix {
public:
template<class T>
MyMatrix(T & begin, T & end) : data(begin, end) {}
int getItem(int i, int j) const {
return data[i*ColumnCount+j];
}
private:
std::vector<int> data;
};
然后,您可以像這樣讀取數據:
std::ifstream file1("pro.txt");
std::istream_iterator<int> begin(file1);
std::istream_iterator<int> end;
MyMatrix<4> m(begin, end);
使用fstream時,為了進行可靠的編碼,最好在open()之后使用is_open()檢查錯誤情況,並在operator <<()之后使用fail()檢查錯誤情況。
此外,更喜歡
while(getline(fil1, lineString))
{
...;
}
因此您可以檢查正在閱讀的行以及出了什么問題。
檢查愉快...
如我所見,您想從文件中加載矩陣。 在文件中,您的值存儲為字符串,並用空格分隔。 因此,您應該加載文件,逐行讀取文件,將字符串分成字符串數組,然后將值從string轉換為int並將其存儲到矩陣中。
每次操作后流的狀態如何? 未經驗證,您不應該閱讀。 而且,您不應該在未確認開放有效的情況下進行閱讀:
ifstream fill( ip_filename.c_str() );
if ( !fill ) {
// error handling, open failed...
}
之后,我同意逐行閱讀的建議:
int row = 0;
string line;
while ( getline( fill, line ) && row < size( pb ) ) {
istringstream sLine( line );
int col = 0;
int tmp ;
while ( sLine >> tmp && col < size( pb[ row ] )) {
pb[row][col] = tmp;
++ col;
}
if ( col != size( pb[ row ] ) ) {
// Input error, too few entries
} else if ( sLine >> ws && sLine.get() != EOF ) {
// Input error, garbage at end of line <row>
}
++ row;
}
if ( row != size( pb ) ) {
// Input error, garbage at end of file
}
或者,您可以根據輸入動態決定尺寸:
std::vector<std::vector<int> > pb;
ifstream fill( ip_filename.c_str() );
if ( !fill ) {
// error handling, open failed...
}
string line;
while ( getline( fill, line ) ) {
std::vector<int> tmp1;
istringstream sLine( line );
int tmp2;
while ( sLine >> tmp2 ) {
tmp1.back().push_back( tmp2 );
}
if ( sLine >> ws && ! sLine.eof() ) {
// input error: non-numeric data in line
} else if ( ! pb.empty() && tmp1.size() != pb.back().size() ) {
// input error : inconsistent number of columns.
}
}
// Check here if square matrix required.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.