如何从文件中读取输入?
我正在尝试将文件中的输入读取到数组中。我似乎已经完成了必要的工作,但代码没有按预期工作。请告诉我哪里错了。这是我的代码:
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。
I am trying to read input from file into an array. I seem to have done the needful, yet the code is not working as it should. Please tell me where am i going wrong. This s my code:
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;
}
The text file is in the same directory as the cpp file is. While printing the output, it just prints 0 irrespective of the value in the file.
The values in the file is store as follows
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
num_rows
and num_cols
are defined previously in the code, both have the value 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这段代码对我来说效果很好,pro.txt 的格式如您所示:
我的建议是确保 pro.txt 与您的 .exe 文件位于同一目录中。如果您使用 IDE 构建此代码,它可能与 .cpp 文件位于不同的目录。
This code works perfectly fine for me with pro.txt formatted like you show:
My suggestion would be to ensure that pro.txt is in the same directory as you .exe file. If you are using an IDE to build this code it is likely a different directory from your .cpp files.
使用 fstream 时,为了实现鲁棒性编码,最好在 open() 之后使用 is_open() 检查错误条件,在operator<<() 之后使用fail() 检查错误条件。
此外,
您最好可以检查正在阅读的行以及出了什么问题。
快乐检查...
When using fstream, for robust coding, it is best to check error conditions with is_open() after open() and fail() after operator<<().
Furthermore, prefer
so you can check what line you're reading in and what is going wrong.
Happy checking...
正如我所见,您想从文件加载矩阵。在文件中,您的值存储为以空格分隔的字符串。因此,您应该加载文件,逐行读取文件,将字符串分成字符串数组,然后将值从字符串转换为 int 并将它们存储到矩阵中。
As i see you want to load a matrix from a file. In file your values are stored as string separated by space. So you should load your file, read the file line by line, separate your string into a string array and the convert your values from string to int and store them into your matrix.
通常,执行此类操作的最简单方法是将数据存储在平面数组中(或者更好的是
std::vector
),并使用简单的算术按行和列访问元素。这使得事情变得更加简单。其包装器可能如下所示:
然后您可以像这样读取数据:
Usually the simplest way to do these kinds of things is to store the data in a flat array (or even better a
std::vector
), and the use simple arithmetics to access the elements by rows and columns. This makes things much simpler.A wrapper for this could look like this:
Then you can read the data like this:
每次操作后流的状态是什么?你不应该这样
读取而不验证。而且你不应该在没有阅读的情况下阅读
验证开放工作:
之后,我同意逐行阅读的建议:
或者,您可以根据动态决定尺寸
到输入:
What is the status of the stream after each operation? You shouldn't be
reading without verifying. And you shouldn't be reading without having
verified that the open worked:
After that, I would agree with the suggestion of reading line by line:
Alternatively, you could decide on the dimensions dynamically, according
to the input: