用C++创建一个带有外部文件的数组

Create an array with external file in C++

本文关键字:外部 文件 数组 一个 创建 C++      更新时间:2023-10-16

我有4天的C++培训,所以请耐心等待。

评估多项选择题考试需要两个数据文件。第一个文件(小册子.dat)包含正确答案。问题总数为50个。一个样品文件如下:

ACBAADDBCBDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD

第二个文件(answer.dat)包含学生的答案。每行有一个学生包含以下信息的记录:

学生的答案(共50个答案)与上述格式相同(*表示没有答案)。,然后是学生ID和学生姓名。示例:

AACCBDBC*DBCBDAAABDBCBDBAA*BCBDD*BABDBCDAABDCBDBDA 6555 MAHMUT
CBBDBC*BDBDBDBABABABBBBBABBABBBBD*BBBCBBDBABBBDC** 6448 SINAN
ACB*ADDBCBDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD 6559 CAGIL

我有一项家庭作业,要写一个C++程序,统计每个学生的正确答案总数,并将这些信息输出到另一个名为report.dat的文件中。在这个文件中,必须给出学生的ID、姓名和分数。每个正确答案得1分。对于上面给出的示例文件,输出应该如下:

6555 MAHMUT 10
6448 SINAN 12
6550 CAGIL 49 

到目前为止,我拥有的是:

include <iostream>
include <fstream>
using namespace std;
int main()
{
    char booklet[50] answers[50]
    int counter
    // Link answers with booklet.dat
    booklet = ifstream
    input_file("booklet.dat");
    return 0;
    // Link answers with answers.dat
    answers = ifstream
    input_file("answerswer.dat");
    return 0;

    while (booklet==answers)
    {
        counter++
        cout << "The student had">>counter>> "answerswers right";
    }
}

我甚至不确定自己的方向是否正确。我知道我需要从文件booklet.dat创建一个数组,从文件answer.dat创建另一个数组。然后必须进行比较,并计算两者之间的匹配。

我不希望有人帮我做这项任务,我只需要朝着正确的方向努力。

1.)关于你的语法:

a) C++中的每一行都必须以";"结尾。你的辩解中有几行没有。(通常情况下,您的编译应该指向这一行或带有错误的下一行)

b) 多个变量定义需要两个不同变量之间的","。

2.)我建议你使用这样的东西:(看看C++参考流)编辑:只是一个小大纲,在这种形式下并不完整,只是为了给你和想法;-)

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int nr_of_students = 1000;   /* Or any number you'd like to analyze */
int stud_nr[nr_of_students];
string stud_name[nr_of_students];
int stud_count[nr_of_students];
fstream in_out;
in_out.open("filename.dat",fstream::in); // fstream::in for reading from file
                                         // fstream::out for writing to this file
if(in_out.is_open())
{
    for(lines=0;(in_out>>answers && lines<nr_of_students);lines++)
    {
         in_out >> stud_nr[lines];   /* EDIT: sorry hat some index confusions here... */
         in_out >> stud_name[lines];
         stud_count[lines]=0;
         for(int i=0;i<50;i++)
         {
              /* comparison between the booklet_array and the answers_array */
              /* Count up the stud_count[lines] for each right comparison */
         }
    }
    /* some simmilar code for the output-file */
}
else cout << "Error reading " << "filename.dat" << endl;
return 1;
}

3.)使用向量,您的代码也会获得更高的性能。一个好的教程是:教程第一部分你可以在的评论中找到第2部分

4.)您可以使用argc和argv**实现更动态的代码,只需在谷歌上搜索即可

我希望这些评论能帮助你继续前进;)

您已经朝着正确的方向前进了。基本上,你想把答案键加载到一个数组中进行快速比较,然后你需要检查每个学生的答案,每次他们得到正确答案时,你都会增加一个计数器,并写下每个学生的ID、姓名和分数。您的代码存在问题,例如缺少分号。另外请注意,返回将退出函数,并且在无条件返回后不会执行任何语句,从main返回将终止程序。

打开文件进行读取的正常方法是:

#include<fstream>
#include<string>
int main()
{
   std::ifstream input_file("inputfilename");
   // since the answer key is one line
   // and each students answer , id and name are also one line 
   // getting that line using std::getline() would be sufficient
   std::string line;
   std::getline(input_file, line);
   // line would now contain the entire first line except the newline character
   std::getline(input_file, line);
   //now line would now contain the second line in the file
   return 0;
}

写入文件类似于我们使用ofstream打开文件进行写入。

像这样:

#include<fstream>
int main()
{
   std::ofstream output_file("outputfilename");
   // lets say we have a string and an int that we want to write
   std::string line_to_write("Hello File");
   int number = 42;
   output_file << line_to_write << number; // writes the string and then 42 on the same line
   output_file << 'n'; // writes the newline character so that next writes would appear on another line
   return 0;
}

对于标准库和C++的引用,当你需要知道可用的函数来做一些事情时,我建议cppreference这里是关于ifstream和ofstream的特定页面。