将几种类型的数据读入数组
Reading several types of data into arrays
我很难弄清楚将文本文件读取到程序中的过程。在get_answer_key_array中,我试图读取并仅将文本文件的顶行放入数组中。
文本文件看起来像这样:
ABDBCBDBABABCBABCB
Bob Bobby adcad abcbd bacb
在这个程序中测试的每个文本文件将在第一行有答案键。第一行之后的每一行都有人名,空格,姓氏,空格,成绩,当我找到它时,缺失的成绩将被"-"替换。
我目前正在努力获得第一行并将其放入答案键数组。我知道,一旦我完成了第一行,我就可以将人名、姓氏和答案放入三个独立的并行数组中。我很难想出正确的方法来检查第一行,这样我就可以得到答案键。
好了,现在我已经改变了我的get_answer_key_array来获得我需要的所有数组。目前,我正试图将顶行(答案键)放入answer_key[]的第一个数组。我试图实现getline函数,但我试图弄清楚如何只得到顶行。是否有可能保持我的eof()循环,但在第一个结束行停止,将第一行的数据传输到数组中?还我Answer_key [i] = key;我打赌需要换别的东西!
我还应该提到,一旦我弄清楚如何将顶行放入数组,我想使用这个过程通过以下工作流程将其余的数据(姓名和答案)放入它们自己的单独数组中:
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();
}
你可以简单地这样读第一行
void get_answer_key_array(ifstream &in_stream, char *answer_key, int &count) {
in_stream >> answer_key;
count = strlen(answer_key);
}
answer_key数组必须是char类型的。
结尾字符是'n'而不是'/n'
相关文章:
- C++将文本文件中的数据读取到结构数组中
- 通过JNI传递数据数组的最快方法是什么
- C++数据文件、数组和计算赋值
- 使用指针访问数组中的对象数据成员
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 很好的语法来获取对向量/数组数据的大小引用?
- 创建异构顶点数据数组的可移植方法
- 模板 使用数据数组调用函数时扩展参数包
- 如何在 c++ 和 java 之间通过 udp 有效地发送数据数组
- 如何使用模板函数从缓冲区(T* 数据数组)创建 cv::Mat
- 如何连接 2D 数据数组
- 如何使用GStreamer播放PCM数据数组
- P/Invoke使用C#和C++之间的双编组数据数组
- 如何在文件中保存/读取数据数组 (C++)
- Android & JNI 如何将 Byte[] 数据数组传递给 JNI 并取回 Byte []
- C++ 通过多次重复向量来构建数据数组
- 尝试使用pthreads访问共享数据数组时,出现“无法访问内存地址”错误
- Qt存储和访问大数据数组
- 在c++中初始化一些数据数组