C++ 读取文件并将其内容存储到二维数组C++;
我一直在尝试创建一个函数,该函数读取名为“test.txt”的文件,并将其内容存储到一个二维数组中,以便在其他函数中使用 它似乎从文本文件中读取了一些内容,但它只在test.txt数字应该显示的区域重复“-9.25996e+61” 任何帮助都将不胜感激,谢谢 “test.txt”文件包含以下12个元素。它们在文档中垂直对齐: 我的代码是:C++ 读取文件并将其内容存储到二维数组C++;,c++,arrays,file,C++,Arrays,File,我一直在尝试创建一个函数,该函数读取名为“test.txt”的文件,并将其内容存储到一个二维数组中,以便在其他函数中使用 它似乎从文本文件中读取了一些内容,但它只在test.txt数字应该显示的区域重复“-9.25996e+61” 任何帮助都将不胜感激,谢谢 “test.txt”文件包含以下12个元素。它们在文档中垂直对齐: 我的代码是: #include <iostream> #include <fstream> using namespace std; //S is
#include <iostream>
#include <fstream>
using namespace std;
//S is the # of students and T is the number of test scores per student
const int S = 3;
const int T = 4;
void getTests(double[][T]);
int main()
{
double testScores[S][T];
getTests(testScores);
}
//*******************
//Gets test scores
void getTests(double testScores[S][T])
{
ifstream testFile("test.txt");
double score;
for (int student = 0; student < S; student++)
{
cout << "Student " << student + 1 << endl;
for (int test = 0; test < T; test++)
{
testFile >> score;
cout << "Test " << test + 1 << endl;
cout << score << endl;
}
}
}
输出提示您尝试使用
ifstream testFile(“test.txt”)打开文件代码>失败。您从不验证文件是否已打开,并且无法验证每次读取,因此无法知道您的文件是否从未打开
处理输入时,必须验证每个操作。您可以通过验证流状态来实现这一点,该流状态提供了成员函数,允许您通过成员函数.good()、.eof()、.fail()和.bad()
检查流状态。您应该在尝试打开文件后立即验证流状态。可以显式地使用.good()
,也可以隐式地验证每次尝试读取文件是否成功
在您的情况下,您可以:
/* read test scores from file 'fname' */
void getTests(double testScores[][T], std::string fname)
{
std::ifstream testFile (fname); /* open file */
double score;
if (!testFile.good()) { /* validate file is open for reading */
std::cerr << "error: file open failed - " << fname << '\n';
return;
}
上面的读取循环以test
和testFile>>分数为条件。如果读取分数失败,则循环退出。类似地,由于使用嵌套循环来读取一定数量的学生,因此外部循环只会增加,而testFile.good()
保持为true
总之,您的简短示例是:
#include <iostream>
#include <fstream>
#include <string>
const int S = 3; /* number of students */
const int T = 4; /* number of scores per-student */
void getTests(double[][T], std::string fname); /* pass filename as parameter */
int main (int argc, char **argv) {
double testScores[S][T];
std::string filename = argc > 1 ? argv[1] : "test.txt";
getTests (testScores, filename);
}
/* read test scores from file 'fname' */
void getTests(double testScores[][T], std::string fname)
{
std::ifstream testFile (fname); /* open file */
double score;
if (!testFile.good()) { /* validate file is open for reading */
std::cerr << "error: file open failed - " << fname << '\n';
return;
}
for (int student = 0; testFile.good() && student < S; student++) {
int test = 0;
std::cout << "Student " << student + 1 << '\n';
while (test < T && testFile >> score) { /* validate EVERY input */
testScores[student][test] = score; /* store value in array */
std::cout << "Test " << ++test << '\n' << score << '\n';
}
}
}
示例使用/输出
$ ./bin/studentscores dat/test12elements.txt
Student 1
Test 1
70
Test 2
80
Test 3
90
Test 4
75
Student 2
Test 1
85
Test 2
90
Test 3
60
Test 4
80
Student 3
Test 1
80
Test 2
90
Test 3
80
Test 4
75
仔细检查一下,如果你还有其他问题,请告诉我。另外,不要将分数存储在一个普通的“代码>双< /代码>数组中,考虑使用或替换。您甚至可以创建一个短结构或std::unordered_set
来存储学生姓名以及考试分数的数组或向量。首先,测试testFile.good()
以查看是否成功打开了文件。这可能是您的问题。我认为您根本没有从文件中读取内容。看起来不像ifstream testFile(“test.txt”)代码>成功。您必须通过检查testFile.good()
或验证每个读取操作if(!(testFile>>score)){/*handle error*/}
来验证它是否有效,我确信这会起作用,我知道为什么会这样,但打开文件时仍然存在问题。测试文件时,它总是导致错误。我甚至完全按照你的例子做了,但仍然不起作用。该文件位于我的Visual Studio项目的资源文件中。我会继续努力,希望明天能拿到。如果我发布了工作代码,我会发布。(1)你在使用IDE吗?(2) 如果是这样,我怀疑您有路径问题。如果使用的是命令行,则可以在命令行上指定路径。让我知道我们可以解决这个问题。很好,你有VS2017。看见你还有另一个选择。在VS2017菜单文件夹中,打开VS Developers命令提示符
。这使您可以完全自由地使用VS2017的命令行。现在只需将cd\Users\you\path\to\your\project
放入test.txt
。然后只需运行nameofproject.exe
test.txt`它就会找到您的文件。这就是为什么我讨厌新程序员使用IDE。你没有卡在C上,你卡在了我如何配置VS?
(很抱歉,我女儿当时正在玩联盟,一个虚假的IPv6数据包锁定了路由器——有2个路由器(智能交换机)、一个WLAN/wifi路由器和3个集线器,用白色来找出哪个被锁定了……)该文件可能与您的项目位于同一文件夹中。默认调试设置将工作目录设置为$(ProjectDir)
for (int student = 0; testFile.good() && student < S; student++) {
int test = 0;
std::cout << "Student " << student + 1 << '\n';
while (test < T && testFile >> score) { /* validate EVERY input */
testScores[student][test] = score; /* store value in array */
std::cout << "Test " << ++test << '\n' << score << '\n';
}
}
}
#include <iostream>
#include <fstream>
#include <string>
const int S = 3; /* number of students */
const int T = 4; /* number of scores per-student */
void getTests(double[][T], std::string fname); /* pass filename as parameter */
int main (int argc, char **argv) {
double testScores[S][T];
std::string filename = argc > 1 ? argv[1] : "test.txt";
getTests (testScores, filename);
}
/* read test scores from file 'fname' */
void getTests(double testScores[][T], std::string fname)
{
std::ifstream testFile (fname); /* open file */
double score;
if (!testFile.good()) { /* validate file is open for reading */
std::cerr << "error: file open failed - " << fname << '\n';
return;
}
for (int student = 0; testFile.good() && student < S; student++) {
int test = 0;
std::cout << "Student " << student + 1 << '\n';
while (test < T && testFile >> score) { /* validate EVERY input */
testScores[student][test] = score; /* store value in array */
std::cout << "Test " << ++test << '\n' << score << '\n';
}
}
}
$ cat dat/test12elements.txt
70
80
90
75
85
90
60
80
80
90
80
75
$ ./bin/studentscores dat/test12elements.txt
Student 1
Test 1
70
Test 2
80
Test 3
90
Test 4
75
Student 2
Test 1
85
Test 2
90
Test 3
60
Test 4
80
Student 3
Test 1
80
Test 2
90
Test 3
80
Test 4
75