我很难搞清楚将文本文件读入程序的过程。在get_answer_key_array中,我试图读取并只将文本文件的顶行放入数组中。
文本文件如下所示:
ABDBCBDBABABCBABCB
Bob Bobby adcad abcbd bacb
在此程序中测试的每个文本文件都将在第一行显示答案键。第一行之后的每一行都会有人名,空格,姓氏,空格,成绩和缺失等级,当我到达时,将替换为“ - ”。
我目前正致力于获取第一行并将其放入答案键数组中。 我知道,一旦完成第一行,我就可以将人名,姓和答案分成三个独立的并行数组。我无法找到正确的方法来检查第一个新行,以便我可以得到答案密钥。
好吧,现在我已经更改了我的get_answer_key_array以获取我需要的所有数组。目前我试图将第一行(答案键)放入answer_key []的第一个数组中。我试图实现getline函数,但我想弄清楚如何只获得顶线。是否有可能保持我的eof()循环停止在第一个终端,以将第一行的数据传输到数组中?还有我的 answer_key [i] =密钥;需要改成其他我打赌的东西!
我还应该提一下,一旦我弄清楚如何将顶线放入数组,我想通过以下工作流程使用此过程将其余数据(名称和答案)放入各自的独立数组中: / p>
in_stream >> first_name[i] >> last_name[i] >> answers[i];
while(!in_stream.eof() ) {
i++;
in_stream >> first_name[i] >> last_name[i] >> answers[i];
}
in_stream.close();
以下计划开始
void get_input_file(ifstream &in_stream); //gets the text file name from the user
void get_arrays(ifstream &in_stream, int answer_key[], string first_name[], string last_name[], int answers[], int &count); //brings the data from text file into all of the parallel arrays
//void score_grader(int &target, string first_name[], string last_name[], int answers[], int &count, int &score);
//void letter_grade(int &score, int &letter_grade);
//void student_report(int &target, string first_name[], string last_name[], int answers []);
int main()
{
ifstream in_stream;
int answer_key[30], count = 0, score = 0; //initializing the answer key array up to 30 answers
string first_name[20]; //initializing the first name array
string last_name[20]; //initializing the last name array
int answers[30]; //initializing the answers array
cout << "Welcome to the Test Grader." << endl; //welcome message
get_input_file(in_stream); //function call to get the file name
get_arrays(in_stream, answer_key, first_name, last_name, answers, count); //function call to create the arrays
}
void get_input_file(ifstream &in_stream) {
string file_name; //initializing the file name string
do {
cout << "Enter the file name you would like to import the data from: " << endl; //asks user to input name of file
cin >> file_name; //user types name of file
in_stream.open(file_name.c_str()); //program opens the stream
if(in_stream.fail()) {
cout << "Error finding file, try again.\n"; //if failed, asks user for file name again
continue; //continues back to the do loop
}
break;
} while(true);
cout << "File Obtained: " << file_name << endl; //alerts user of file success with printed name
}
void get_arrays(ifstream &in_stream, int answer_key[], string first_name[], string last_name[], int answers[],
int &count) {
int i = 0;
string key; //This will be the storage variable for the first line of text file
if (in_stream.is_open() ) { //if the stream is open
getline(in_stream, key);
cout << "Testing: " << key << endl;
while(!in_stream.eof() ) {
i++;
in_stream >> first_name[i] >> last_name[i] >> answers[i];
}
}
cout << first_name[1] << " " << last_name[1] << " " << answers[1] << endl;
in_stream.close();
}
答案 0 :(得分:0)
你可以这样简单地阅读第一行
void get_answer_key_array(ifstream &in_stream, char *answer_key, int &count) {
in_stream >> answer_key;
count = strlen(answer_key);
}
answer_key数组必须是char。
类型终结字符是&#39; \ n&#39;不是&#39; / n&#39;