C++ 返回到文件开头后,循环中的ifstream停留在相同的值上
我正在尝试编写一个函数,从文件中读取值并将其放入矩阵中。通过扫描文件中的行数并将该行数用作矩阵中的行数,可以生成矩阵(由两列组成)。要读取这些值,ifstream对象C++ 返回到文件开头后,循环中的ifstream停留在相同的值上,c++,C++,我正在尝试编写一个函数,从文件中读取值并将其放入矩阵中。通过扫描文件中的行数并将该行数用作矩阵中的行数,可以生成矩阵(由两列组成)。要读取这些值,ifstream对象读取器将返回到文件的开头。但是,这样做之后,reader在整个循环中停留在一个整数上(我认为这是一个垃圾值)。动态分配矩阵的函数运行良好 我将MCVE包括在下面 #include <fstream> #include <iostream> #include <string> using name
读取器
将返回到文件的开头。但是,这样做之后,reader
在整个循环中停留在一个整数上(我认为这是一个垃圾值)。动态分配矩阵的函数运行良好
我将MCVE包括在下面
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
string fileChoice;
cout << "Choose a file to open: ";
cin >> fileChoice;
ifstream reader;
reader.open(fileChoice);
if (reader.fail()){
cerr << fileChoice << " could not be opened" << endl;
system("pause");
exit(1);
}
// https://stackoverflow.com/questions/26903919/c-allocate-dynamic-array-inside-a-function
int** Matrix = new int*[4];
for (int i = 0; i < 4; i++) {
Matrix[i] = new int[2];
}
reader.seekg(0, ios::beg);
for (int i = 0; i < 4; i++){
for (int j = 0; j < 2; j++){
reader >> Matrix[i][j];
cout << Matrix[i][j] << " ";
}
}
system("pause");
exit(0);
}
这是我所期望的cout
的输出:
1 10 2 10 11 20 23 30
但我得到的却是:
-842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451
另外,当改变
reader.seekg(0, ios::beg);
for (int i = 0; i < 4; i++){
for (int j = 0; j < 2; j++){
reader >> Matrix[i][j];
cout << Matrix[i][j] << " ";
}
}
当我获取问题中的代码,并添加
reader.clear()
要获取此信息,请执行以下操作:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
string fileChoice;
cout << "Choose a file to open: ";
cin >> fileChoice;
ifstream reader;
reader.open(fileChoice);
if (reader.fail()){
cerr << fileChoice << " could not be opened" << endl;
system("pause");
exit(1);
}
// https://stackoverflow.com/questions/26903919/c-allocate-dynamic-array-inside-a-function
int** Matrix = new int*[4];
for (int i = 0; i < 4; i++) {
Matrix[i] = new int[2];
}
reader.clear();
reader.seekg(0, ios::beg);
for (int i = 0; i < 4; i++){
for (int j = 0; j < 2; j++){
reader >> Matrix[i][j];
cout << Matrix[i][j] << " ";
}
}
}
您从未检查
读卡器的状态?请按要求在此处发布一个复制您的问题的帖子。-84215051
是0xcdcdcdcd=未初始化的堆内存在调用seekg()
之前,您是否调用了reader.clear()
?如果没有,那么先加上-84215051听起来像是联合化了。您确定阵列的大小正确吗?你能告诉我们你是如何创造它的吗?(或更好,如πάντα)ῥεῖ 已经说过,做一个MVCE)@JerryCoffin是的,但同样的错误持续存在我不明白reader.clear()
是如何解决这个问题的。ifstream
对象将引发哪些标志?
int beg;
reader.seekg(0, ios::beg);
for (int i = 0; i < 4; i++){
for (int j = 0; j < 2; j++){
reader >> beg;
cout << beg << " ";
}
}
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
string fileChoice;
cout << "Choose a file to open: ";
cin >> fileChoice;
ifstream reader;
reader.open(fileChoice);
if (reader.fail()){
cerr << fileChoice << " could not be opened" << endl;
system("pause");
exit(1);
}
// https://stackoverflow.com/questions/26903919/c-allocate-dynamic-array-inside-a-function
int** Matrix = new int*[4];
for (int i = 0; i < 4; i++) {
Matrix[i] = new int[2];
}
reader.clear();
reader.seekg(0, ios::beg);
for (int i = 0; i < 4; i++){
for (int j = 0; j < 2; j++){
reader >> Matrix[i][j];
cout << Matrix[i][j] << " ";
}
}
}
1 10 2 10 11 20 23 30