C++ 如何将内容数组写入文本文件?
代码应该打开一个现有的文本文件,将内容传输到数组中,然后创建一个新的文本文件,然后将数组内容写入新的文本文件中。我遇到的问题是,代码只输出新文本文件中最后一行内容C++ 如何将内容数组写入文本文件?,c++,arrays,C++,Arrays,代码应该打开一个现有的文本文件,将内容传输到数组中,然后创建一个新的文本文件,然后将数组内容写入新的文本文件中。我遇到的问题是,代码只输出新文本文件中最后一行内容 file.open("Patient.txt", ios::in); while (!file.eof()) { file >> arr[i].name >> arr[i].DOB >> arr[i].address >> arr[i].Dr_name >&
file.open("Patient.txt", ios::in);
while (!file.eof()) {
file >> arr[i].name >> arr[i].DOB >> arr[i].address >> arr[i].Dr_name >> arr[i].V_date;
/*cout << arr[i].name << arr[i].DOB << arr[i].address << arr[i].Dr_name << arr[i].V_date << endl;*/
}
file.close();
files.open("Patients_2.txt");
if (files.is_open()) {
for (i; i < 30; i++) {
files << arr[i].name << arr[i].DOB << arr[i].address << arr[i].Dr_name << arr[i].V_date;
}
}
files.close();
patientss.open("Patients_2.txt");
cout << "Patient 2: " << endl;
while (!patientss.eof()) {
getline(patientss, line);
cout << line << endl;
}
patientss.close();
system("pause");
return 0;
}
此循环有几个问题:
while (!file.eof()) {
file >> arr[i].name >> arr[i].DOB ....
您从不增加i,因此相同的arr[i]将被一次又一次地覆盖。
你用!file.eof作为停止读取的条件。在您尝试读取文件末尾以外的内容之前,eof不会被设置,这意味着如果您按照应该的方式增加i,则最后一个arr将为空/中断。而是检查从流中提取是否成功。因为流是在执行stream>>var时返回的,并且具有显式运算符bool const的重载,该运算符返回!失败您可以直接在循环中执行此检查:
而流>>变量{提取成功}
但是,对可能包含空格的字符串字段使用格式化输入>>不是一个好主意。你们的名字,农库尔,会被分开,所以农将进入名字,库尔将进入DOB。最好使用不太可能包含在任何人姓名中的字段分隔符\n通常很好,并且可以与std::getline一起使用
getline返回您作为参数给出的流,这意味着您可以像stream>>var1>>var2一样链接getlines,只是它有点冗长。
getlinegetlinestream、var1、var2将第一行放在var1中,第二行放在var2中
为了简化输入和输出,可以为数据类型添加流运算符,并使输入流运算符对字段使用getline
例如:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct data_t {
std::string name;
std::string DOB;
std::string address;
std::string Dr_name;
std::string V_date;
};
// input stream operator using chained getlines
std::istream& operator>>(std::istream& is, data_t& d) {
using std::getline;
return getline(getline(getline(getline(getline(is,
d.name), d.DOB), d.address), d.Dr_name), d.V_date);
}
// output stream operator
std::ostream& operator<<(std::ostream& os, const data_t& d) {
return os << d.name << '\n'
<< d.DOB << '\n'
<< d.address << '\n'
<< d.Dr_name << '\n'
<< d.V_date << '\n';
}
int main() {
std::vector<data_t> arr;
if(std::ifstream file("Patient.txt"); file) {
data_t tmp;
while(file >> tmp) { // remember, no eof() needed
arr.push_back(tmp);
}
}
if(std::ofstream file("Patients_2.txt"); file) {
for(const data_t& d : arr) {
file << d;
}
}
if(std::ifstream patientss("Patients_2.txt"); patientss) {
data_t tmp;
while(patientss >> tmp) {
std::cout << tmp;
}
}
}
IMHO,您应该在patient类中重载格式化的插入和提取运算符:
struct Patient
{
std::string name;
std::string dob;
std::string address;
std::string dr_name;
std::string v_date;
friend std::istream& operator>>(std::istream& input, Patient& p);
friend std::ostream& operator<<(std::ostream& output, const Patient& p);
};
std::istream& operator>>(std::istream& input, Patient& p)
{
std::getline(input, p.name);
std::getline(input, p.dob);
std::getline(input, p.address);
std::getline(input, p.dr_name);
std::getline(input, p.v_date);
return input;
}
std::ostream& operator<<(std::ostream& output, const Patient& p)
{
output << p.name << "\n";
output << p.dob << "\n";
output << p.address << "\n";
output << p.dr_name << "\n";
output << p.v_date << "\n";
return output;
}
上述功能使输入和输出更容易:
std::vector<Patient> database;
Patient p;
while (input_file >> p)
{
database.push_back(p);
}
const unsigned int quantity = database.size();
for (unsigned int i = 0; i < quantity; ++quantity)
{
output_file << database[i];
}
上面的代码还支持封装和数据隐藏的概念。Patient结构负责或读取其成员,因为它知道成员的数据类型。患者外部的代码只关心患者实例的输入和输出,而不关心内部 读取文件的第一行时,i的值是多少?当您读取文件的最后一行时?当你读到最后一行时?你听说过计算机编程的黄金法则吗?你的计算机总是完全按照你的吩咐去做,而不是按照你的要求去做?你能一步一步地解释一下,当你告诉你的电脑做什么的时候,它到底是怎么做的吗?然后,确切地说,你的for循环告诉你的计算机做什么?while!patientss.eof{避免这种模式当你读取文件的第一行时i的值是多少?当你读取文件的最后一行时?当你读取最后/2行时?不相关:一旦你让其余的工作正常,文件>>arr[i]。名称>>arr[i]。DOB>>arr[i]。地址>>arr[i]。医生姓名>>arr[i].V_date;将从文件中读取单词,因此您会收到垃圾,例如患者的姓氏被读入DOB。您需要更多的智能。类似的文件