C++ 如何在文本文件中存储信息(字符串),然后甚至在C+中读取它+;
基本上,我想在我的程序中添加一个关于客户的信息。我想在文本文件中存储一个信息(名称、数字和最多50个字符的字符串注释)并关闭该程序。然后,如果我打开了该程序,我想搜索一个名称并添加它,然后再显示我存储的所有信息 如果有人能帮我,我将不胜感激。(这是一个原型)C++ 如何在文本文件中存储信息(字符串),然后甚至在C+中读取它+;,c++,fstream,C++,Fstream,基本上,我想在我的程序中添加一个关于客户的信息。我想在文本文件中存储一个信息(名称、数字和最多50个字符的字符串注释)并关闭该程序。然后,如果我打开了该程序,我想搜索一个名称并添加它,然后再显示我存储的所有信息 如果有人能帮我,我将不胜感激。(这是一个原型) #包括 #包括 #包括 #包括 #包括 int add(); int log(); 使用名称空间std; 结构客户{ 字符串名; 字符串dis; }螺柱; int main(){ INTA; 做{ 请仔细阅读“序列化和反序列化”的概念。然后
#包括
#包括
#包括
#包括
#包括
int add();
int log();
使用名称空间std;
结构客户{
字符串名;
字符串dis;
}螺柱;
int main(){
INTA;
做{
请仔细阅读“序列化和反序列化”的概念。然后解释你想要多少。请仔细阅读“序列化和反序列化”的概念。然后解释你想要多少。
#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
#include <windows.h>
int add();
int log();
using namespace std;
struct customer {
string name;
string dis;
}stud;
int main() {
int a;
do {
cout << "1.Add customer"<<endl;
cout << "2.Search customer"<<endl;
cin >> a;
} while (a < 1 || a>2);
switch (a)
{
case 1: add();
break;
case 2: log();
break;
}
return 0;
}
int log() {
fstream fp;
cout << "Search name" << endl;
cin.ignore(1, '\n');
getline(cin, stud.name);
fp.open(stud.name, ios::in, ios::binary);
fp.read((char*)&stud, sizeof(stud));
fp.open(stud.dis, ios::in, ios::binary);
fp.read((char*)&stud, sizeof(stud));
cout << endl << stud.name << endl << stud.dis << endl;
fp.close();
cout << endl << stud.name << endl << stud.dis << endl;
main();
return 0 ;
}
int add(){
fstream fp;
cin.ignore(1, '\n');
cout << "Add name" << endl;
getline(cin, stud.name);
cout << "Add a comment" << endl;
getline(cin, stud.dis);
fp.open(stud.name, ios::out, ios::binary);
fp.write((char*)&stud, sizeof(stud));
fp.open(stud.dis, ios::out, ios::binary);
fp.write((char*)&stud, sizeof(stud));
fp.close();
main();
return 0;
}