gpt4 book ai didi

c++ - 如何在文本文件中存储信息(字符串),然后甚至在C++中读取信息

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:32 24 4
gpt4 key购买 nike

基本上,我想在程序中添加有关客户的信息。我想在文本文件中存储一个信息(名称,数字和字符串-最多50个字符的注释)并关闭程序。然后,如果我打开程序并想要搜索添加的名称,显示我存储的所有信息。

这是我现在无法工作的地方,如果有人可以帮助我,我将不胜感激。 (这是一个原型)

#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;
}

最佳答案

请注意,我没有使用结构,但是如果需要,应该向该结构添加方法以获取信息。

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm> /// std::copy
#include <iterator> ///std::ostream_iterator<T>

void new_user(const std::string &New_User_Name, const std::string &New_User_Surname, const int &New_User_Age);
template <typename T>
void Display_Vector(const std::vector<T> &Vector_To_Display);
void DisplayInformation(const std::string &Path);

int main()
{
/// name information
std::cout << "Enter the name: ";
std::string New_User_Name;
std::getline(std::cin, New_User_Name);
/// surname information
std::cout << "Enter the surname: ";
std::string New_User_Surname;
std::getline(std::cin, New_User_Surname);
/// age information
int New_User_Age = 0;
std::cout << "Enter the age: ";
std::cin >> New_User_Age; /// You should make some checks

new_user(New_User_Name, New_User_Surname, New_User_Age);
DisplayInformation(New_User_Name + "_" + New_User_Surname + ".txt");
return 0;
}

void new_user(const std::string &New_User_Name, const std::string &New_User_Surname, const int &New_User_Age)
{
/// Writing information about the user into the file
std::ofstream New_User_File;
New_User_File.open(New_User_Name + "_" + New_User_Surname + ".txt", std::ios::app);

New_User_File << "Name: " << "\t\t\t" << New_User_Name << std::endl;
New_User_File << "Surname: " << "\t\t" << New_User_Surname << std::endl;
New_User_File << "Age: " << "\t\t\t" << New_User_Age << std::endl;

New_User_File.close();
}

template <typename T>
void Display_Vector(const std::vector<T> &Vector_To_Display)
{
std::copy(Vector_To_Display.begin(), Vector_To_Display.end(), std::ostream_iterator<T>(std::cout, "\n"));
}

void DisplayInformation(const std::string &Path)
{
std::vector<std::string> User_Information_To_Display; /// the vector is going to be used only for storing the information of the user
std::string Information_Of_User;


std::ifstream User_Information;
User_Information.open(Path);

if(!(User_Information.is_open()))
{
return; /// you can make other stuff in here
}
else
{
while(! User_Information.eof()) /// while we are not at the end of the file
{
std::getline(User_Information, Information_Of_User);
User_Information_To_Display.push_back(Information_Of_User);
}

Display_Vector(User_Information_To_Display);
}

User_Information_To_Display.clear();
User_Information.close();
}

关于c++ - 如何在文本文件中存储信息(字符串),然后甚至在C++中读取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58806281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com