gpt4 book ai didi

c++ - 随机字符进入我用c++写的文件

转载 作者:太空狗 更新时间:2023-10-29 20:06:26 24 4
gpt4 key购买 nike

#include <string.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>



class mobile
{
int id;
float price;
char *model,*brand;
public:
int getId()
{
return id;
}
float getPrice(){return price;}
mobile()
{
brand = new char[5];
model = new char[5];
id = 0;
price = 0.0;
}
void addDetail()
{
cout<<"\nEnter the mobile ID ";cin>>id;
cout<<"\nEnter the Price of mobile ";cin>>price;
cout<<"\nEnter the Brand name of mobile ";cin>>brand;
cout<<"\nEnter the Model of mobile ";cin>>model;
ofstream file("database.txt",ios::app);
char c=' ',n='\n';
file<<id<<c;
file<<price<<c;
file<<brand<<c;
file<<model<<n;
file.close();

}

char* getbrand(){return brand;}
char* getModel(){return model;}
~mobile()
{
delete brand;
delete model;
}

};

void count()
{
int counter = 0;
char c;
ifstream file("database.txt");
file.seekg(0,ios::beg);
if(!file)cout<<"File not present";
while(file)
{
file.get(c);
if(c == '\n')
{
counter++;
}
}
cout<<"The no of entries "<<counter;
file.close();
}

void addMobile()
{
char *model,*brand;

mobile m;
m.addDetail();
}

void EditMobile()
{
fstream file;
char* brand,*model;
file.open("database.txt",ios::in);
brand = new char;
model = new char;
char c;
int id,pos;
float price;
float f;
if(!file)cout<<"File not present";
else
{


file>>id;cout<<"ID :"<<id<<endl;
file>>price;cout<<"Price :"<<price<<endl;
file>>brand;cout<<"Brand :"<<brand<<endl;
file>>model;cout<<"Model :"<<model<<endl;

}

delete brand;
delete model;
file.close();

}

void admin()
{

char* userpassword;
userpassword = new char;
char* pass;
pass = new char;

cout<<"\n\nEnter the Password";
cin>>userpassword;
fstream fin;
fin.open("password.txt",ios::in | ios::out);
if(!fin)
cout<<"\n\nFile does not exist";
else
{
fin>>pass;
if(strcmp(userpassword,pass) == 0)
{
char ch;

count();
clrscr();
cout<<"\n\nAcces Granted!!\n\n\tWelcome";
cout<<"\n\n1.Add a Mobile.";
cout<<"\n2.Edit a Mobile.";
cout<<"\n3.Delete a Mobile.";
cout<<"\n4.View the Database.\n\nEnter Your Choice ";
ch = getch();
switch(ch)
{
case '1':
addMobile();
break;
case '2':
EditMobile();
break;
case '3':
//deleteMobile();
break;

}
}
else
{
cout<<"\n\nAcces Denied";
return;
}
}

fin.close();
delete userpassword;
}
int main()
{
char choice;
clrscr();
cout<<"Welcome to Mobile Store management Program";
cout<<"\n1.Find a Mobile.";
cout<<"\n2.Know Price.";
cout<<"\n3.Know Description.";
cout<<"\n4.Administrator Login.\n\nEnter Your Choice";
choice = getch();
switch(choice)
{
case '1':

break;
case '4':
admin();
break;
}
getch();
return 0;
}

现在在我输入的输出中:

Acces Granted!!

Welcome

1.Add a Mobile.
2.Edit a Mobile.
3.Delete a Mobile.
4.View the Database.

Enter Your Choice
Enter the mobile ID 1

Enter the Price of mobile 123.34

Enter the Brand name of mobile Nokia

Enter the Model of mobile mynokiamobile

我在 database.txt 文件中得到的输出是:

1 123.339996 Nokia mynoki   ile

中间的空格是这里看不到的随机字符。

最佳答案

您超出了分配给 brandModel 的内存范围。

brand = new char[5];
model = new char[5];

brandmobile 分配了 5 个字节的内存,但您的输入比这更长。

您输入的值是:
brand:6 字节长的“Nokia”(5 + 1 附加字节为 NULL 终止符)&
对于 model:“mynokiamobile”,长度为 14 字节

这会导致未定义的行为
您需要增加这些自由存储(堆)分配。

更好的 C++ 方法是使用 std::string 而不是字符数组,但我不知道这似乎是一项家庭作业练习,也许你不允许使用它们,在我看来这会很蹩脚。

关于c++ - 随机字符进入我用c++写的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7937838/

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