gpt4 book ai didi

c++ - 从 txt 文件中分离并行数组

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:25 25 4
gpt4 key购买 nike

我是一个非常新手的程序员,我正在尝试编写一个程序来读取一个 txt 文件,其中包含 5 名学生的姓名(仅限名字)以及每个学生的四项考试成绩。我试图将名字读入一个名为 students 的数组,然后将分数读入 4 个单独的数组,分别命名为 test1、test2、test3、test4,然后从监视器上显示它。该文件如下所示:

Steve 78 65 82 73

Shawn 87 90 79 82

Annie 92 90 89 96

Carol 72 65 65 60

Kathy 34 50 45 20

我很难分解数组并组织它们。有人能帮我吗?请记住,我是新手,所以我对编程了解不多。

到目前为止,这是我的代码:

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <ctime>
#define over2 "\t\t"
#define over3 "\t\t\t"
#define over4 "\t\t\t\t"
#define down5 "\n\n\n\n\n"


using namespace std;



int main(int argc, char *argv[])
{


ifstream inputFile;


//Constant for max string size
const int SIZE = 5;


//Constant for the scores
string names[SIZE], fileName, line, test1[SIZE], test2[SIZE], test3[SIZE], test4[SIZE];


//input section, user enters their file name
cout << down5 << down5 << over2 << "Please enter your file name: ";
cin >> fileName;
system("CLS");




//open the file containing the responses
inputFile.open(fileName.c_str());
cout << endl;

//kicks you out if file isn't found
if (inputFile)
{
for(int i = 0; i < SIZE; i++)
{
getline(inputFile, line);
names[i] = line;
getline(inputFile, line);
test1[i] = line;
getline(inputFile, line);
test2[i] = line;
getline(inputFile, line);
test3[i] = line;
getline(inputFile, line);
test4[i] = line;
}
inputFile.close();
}
cout << down5 << over3 << "Student\tTest1\tTest2\tTest3\tTest4\n";
cout << over3 << "-------\t-----\t-----\t-----\t-----\n";
for(int i = 0; i < SIZE; i++)
{
cout << over3 << names[i] << endl;
cout << over3 << test1[i] << endl;
cout << over3 << test2[i] << endl;
cout << over3 << test3[i] << endl;
cout << over3 << test4[i] << endl;

}
return 0;
}

最佳答案

让我们看看您要读取的文件的结构:

Steve 78 65 82 73
Shawn 87 90 79 82
Annie 92 90 89 96
Carol 72 65 65 60
Kathy 34 50 45 20

数据的格式可以描述如下:

  • 每一行代表一条“记录”。
  • 每个“记录”包含多个列。
  • 列由空格分隔。

您当前正在对每个 使用getline():

for(int i = 0; i < SIZE; i++)
{
getline(inputFile, line);
names[i] = line;
getline(inputFile, line);
test1[i] = line;
getline(inputFile, line);
test2[i] = line;
getline(inputFile, line);
test3[i] = line;
getline(inputFile, line);
test4[i] = line;
}

...而您实际上想在一行中读取每条记录并将其拆分:

for (int i = 0; i < SIZE; i++)
{
string line;
size_t start = 0;
// For each line, attempt to read 5 columns:
getline(inputFile, line);
names[i] = get_column(line, start);
test1[i] = get_column(line, start);
test2[i] = get_column(line, start);
test3[i] = get_column(line, start);
test4[i] = get_column(line, start);
}

这是您原始代码的修改版本,它按上述方式拆分了每一行:

#include <cctype>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

static string get_column(string line, size_t &pos)
{
size_t len = 0;
// Skip any leading whitespace characters.
while (isspace(line.c_str()[pos])) { ++pos; }
// Count the number of non-whitespace characters that follow.
while (!isspace(line.c_str()[pos+len]) && line.c_str()[pos+len]) { ++len; }
// Extract those characters as a new string.
string result = line.substr(pos, len);
// Update the "start" position (for the next time this function is called).
pos += len;
// Return the string.
return result;
}

int main()
{

const int SIZE = 5;
string names[SIZE], test1[SIZE], test2[SIZE], test3[SIZE], test4[SIZE];

// Ask the user to enter a file name.
cout << "Please enter your file name: ";
string fileName;
cin >> fileName;

// Open the file and read the data.
ifstream inputFile(fileName.c_str());
if (!inputFile.is_open()) { return 0; }

for (int i = 0; i < SIZE; i++)
{
string line;
size_t start = 0;
// For each line, attempt to read 5 columns:
getline(inputFile, line);
names[i] = get_column(line, start);
test1[i] = get_column(line, start);
test2[i] = get_column(line, start);
test3[i] = get_column(line, start);
test4[i] = get_column(line, start);
}

inputFile.close();

// Display the data.
cout << "Student\tTest1\tTest2\tTest3\tTest4" << endl;
cout << "-------\t-----\t-----\t-----\t-----" << endl;

for(int i = 0; i < SIZE; i++)
{
cout << names[i] << "\t";
cout << test1[i] << "\t";
cout << test2[i] << "\t";
cout << test3[i] << "\t";
cout << test4[i] << endl;
}

}

运行该程序会产生以下输出:

Please enter your file name: textfile.txt
Student Test1 Test2 Test3 Test4
------- ----- ----- ----- -----
Steve 78 65 82 73
Shawn 87 90 79 82
Annie 92 90 89 96
Carol 72 65 65 60
Kathy 34 50 45 20

请注意,get_column() 函数处理多个空格或太短的行,因此该文件:

Steve 78     65 82 73
Shawn 87 90
Annie 92 90 89 96
Carol 72
Kathy 34 50 45 20

...产生以下输出:

Please enter your file name: textfile.txt
Student Test1 Test2 Test3 Test4
------- ----- ----- ----- -----
Steve 78 65 82 73
Shawn 87 90
Annie 92 90 89 96
Carol 72
Kathy 34 50 45 20

关于c++ - 从 txt 文件中分离并行数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29737011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com