C++ 文本不';不要保存到文件中
你好,我做了一个程序,在我的程序中,我有一个类客户。 为了将客户保存在计算机上,我创建了一个文件,并用::like name::password::phonenbr分隔客户的每个数据。但我的问题是,如果我在代码的注释中写下这一行,数据将保存到文件中,但是如果我在if()中写下同一行,检查文件是否为空,这没有任何作用,尽管我在编译器中看到这一行没有问题 如果你能帮助我,那将是优雅的C++ 文本不';不要保存到文件中,c++,file,stream,C++,File,Stream,你好,我做了一个程序,在我的程序中,我有一个类客户。 为了将客户保存在计算机上,我创建了一个文件,并用::like name::password::phonenbr分隔客户的每个数据。但我的问题是,如果我在代码的注释中写下这一行,数据将保存到文件中,但是如果我在if()中写下同一行,检查文件是否为空,这没有任何作用,尽管我在编译器中看到这一行没有问题 如果你能帮助我,那将是优雅的 void Shop::Add_Customer() { fstream myfile; myfile.ope
void Shop::Add_Customer()
{
fstream myfile; myfile.open("CustomerFile.txt");
string name, password, phonenbr;
string buffer, delimitor = "::";
system("cls");
cout << "Name of the customer: "; cin >> name;
cout << "Password of the customer: "; cin >> password;
cout << "Phone number of the customer: "; cin >> phonenbr;
if (!myfile.is_open())
{
myfile.open("CustomerFile.txt", ios::out);
}
//myfile << name + delimitor + password + delimitor + phonenbr << endl;
if (myfile.peek() == std::ifstream::traits_type::eof())
{
myfile << name + delimitor + password + delimitor + phonenbr << endl;
}
else
{
while (getline(myfile, buffer))
{
if (CheckIfCustomerExist(buffer, name, phonenbr) == true)
{
cout << "Customer already exist" << endl;
}
else
{
myfile << name + delimitor + password + delimitor + phonenbr << endl;
cout << "Customer insert in the file " << endl;
}
}
}
}
void商店::添加客户()
{
fstream myfile;myfile.open(“CustomerFile.txt”);
字符串名称、密码、phonenbr;
字符串缓冲区,定界符=“::”;
系统(“cls”);
姓名;
cout>密码;
cout>phonenbr;
如果(!myfile.is_open())
{
打开(“CustomerFile.txt”,ios::out);
}
//myfile当流的任何读取失败时,流中的EOF标志被设置,因为它试图读取超过流的结尾。一旦设置EOF,流处于错误状态,在清除EOF标志之前无法读取或写入
下面是一个非常简单的例子:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile("CustomerFile.txt", ios::out);
if (!myfile.is_open())
{
cout << "file not open." << endl;
}
else
{
if (myfile.peek() == std::ifstream::traits_type::eof())
{
if (myfile.eof())
{
cout << "Need to clear the EOF flag." << endl;
}
}
}
}
离题材料:
下面的代码
while (getline(myfile, buffer))
{
if (CheckIfCustomerExist(buffer, name, phonenbr) == true)
{
cout << "Customer already exist" << endl;
}
else
{
myfile << name + delimitor + password + delimitor + phonenbr << endl;
cout << "Customer insert in the file " << endl;
}
}
如果文件不存在,第一次打开调用肯定会失败,迫使第二次调用打开。最好将ios::out
添加到此调用中,然后完成此调用。第二次top open调用可能由于其他原因失败,并且未经测试是否成功,因此我建议
fstream myfile("CustomerFile.txt", ios::out);
if (!myfile.is_open())
{
perror("file not open: ");
}
else
{
// your code goes here
}
问题的根源在于if语句的条件:
(myfile.peek() == std::ifstream::traits_type::eof())
显然,您的文件在以下行中以流媒体模式打开:
fstream myfile; myfile.open("CustomerFile.txt");
现在,我能得到的唯一原因是,为什么你的if语句的条件不满足,是因为文件模式不同。我不确定我是否正确(欢迎在评论框中反馈),但这是我能想出的一个原因
我尝试了一种我自己的方法,这种方法总是有效的,它也适用于您的代码。我在您的代码中替换了以下行:
if (myfile.peek() == std::ifstream::traits_type::eof())
{
myfile << name + delimitor + password + delimitor + phonenbr << endl;
}
fstream myfile; myfile.open("CustomerFile.txt");
此代码是您代码中这些行的重复:
if (myfile.peek() == std::ifstream::traits_type::eof())
{
myfile << name + delimitor + password + delimitor + phonenbr << endl;
}
fstream myfile; myfile.open("CustomerFile.txt");
.open()命令已经完成了我指出的if语句。如果找到了open()中指定的文件,它将打开该文件;否则它将继续创建新文件。因此,if语句是多余的,应该删除,因为它会消耗不必要的CPU功率并减慢程序的速度(不是很多,但您很快就会意识到,在运行代码时,每毫秒都很重要;效率是关键)
另一个问题是您接受输入的3个变量。既然它们是字符串,为什么要使用cin>>方法?使用cin只接受句子中的第一个单词;在您的下一行:
cout << "Name of the customer: "; cin >> name;
这个函数将把所有的单词和空格作为一个句子,直到你点击回车键为止,不像cin那样只获取第一个单词而忽略句子的其余部分
最后,你的电话号码应该是int型的。我会留给你根据你的要求修改
希望我回答了你的问题,也希望我的建议能对你有所帮助。祝你好运
编辑:关于您的代码,我遗漏的另一点是,您的while循环对每一行都运行。这意味着它将在文件的每一行检查特定客户的名称。这不是您想要的。您希望读取文件中的每一行,但如果您找到了客户,则您希望终止该函数而不继续执行任何操作下一行。另外,您只想在读取整个文件后打印错误语句,而不仅仅是一行
else
{
int check = 0;
while (getline(myfile, buffer))
{
if (CheckIfCustomerExist(buffer, name, phonenbr) == true)
{
cout << "Customer already exist" << endl;
check = 1;
break;
}
}
if (check == 0)
{
myfile << name + delimitor + password + delimitor + phonenbr << endl;
cout << "Customer insert in the file " << endl;
}
}
else
{
整数检查=0;
while(getline(myfile,buffer))
{
if(CheckIfCustomerExist(缓冲区、名称、电话号码)=true)
{
cout off-topic:您会发现编写解析器以使用单个字符分隔符读回文件要容易得多。对open
的第二次调用涵盖了“文件不存在”第一次调用open
时未涉及故障情况。添加的ios::out
将强制创建不存在的文件。因此,这是一次重复,但第二次调用是正确的。关于客户名称的观点很好。不同意电话号码是int
。可能应该保留一个字符串并测试其是否存在正确的位数和所有的输入字符都是数字。<代码> int >代码>不能处理前面的0,例如,实际上,这是真的@ USER881301,我没有考虑电话号码的那个方面。
cout << "Name of the customer: "; cin >> name;
getline(cin, name);
else
{
int check = 0;
while (getline(myfile, buffer))
{
if (CheckIfCustomerExist(buffer, name, phonenbr) == true)
{
cout << "Customer already exist" << endl;
check = 1;
break;
}
}
if (check == 0)
{
myfile << name + delimitor + password + delimitor + phonenbr << endl;
cout << "Customer insert in the file " << endl;
}
}