当我尝试在 c++ 17 上运行此代码时,它可以工作,但在 c++ 上却没有。第一次运行时,它只给我最后输入元素的字符串值,但是当我第二次运行它时,它显示分段错误(核心转储)错误。有人可以帮我弄这个吗?
这是我的代码:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<iomanip>
#include <stdlib.h>
using namespace std;
void show1(string name,string type,string price,string quantity){
cout<<"=======================================\n";
cout<<setw(5)<<name<<setw(15)<<type<<setw(10)<<price<<setw(15)<<quantity<<"\n";
cout<<"========================================\n";
}
template<class T>
void Readfile(T &obj,string fname,string name,string type,string price,string quantity){
int i=1;
ifstream f;
f.open(fname,ios::in);
f.seekg(0);
Display(name,type,price,quantity);
while(!f.eof()){
f.read((char*)&obj, sizeof(obj));
if(!f.eof()) {
if(f.tellg()<0)
{ i=0; break;}
obj.putdata();
}
}
f.close();
}
class Items{
string name;
double price;
int quantity;
public:
string itemObj;
void getdata(string obj){
cout<<"Enter name, price and quantity: \n";
cin>>name>>price>>quantity;
itemObj=obj;
}
void putdata(){
cout<<setw(5)<<name<<setw(10)<<itemObj<<setw(12)<<price<<setw(10)<<quantity<<"\n";
}
void writeToFile(int n,string obj);
void ReadFile_item();
int check(string temp_name);
void refill_Item(int qty);
double checkout_price(int qty);
}item;
class CD:public Items{
public:
void Add_CD();
}cd;
double Items::checkout_price(int qty){
if(quantity>=qty){
quantity-=qty;
cout<<"File Updated";
return price*qty;
}
else{
cout<<"\nNot enough stock";
return 0;
}
}
void Items::writeToFile(int n,string obj){
ofstream f;
f.open("stock.txt",ios::out|ios::app);
for(int i=0;i<n;i++){
item.getdata(obj);
f.write((char*)&item, sizeof(item));
cout<<"File Updated\n";
}
f.close();
}
void Items:: ReadFile_item(){
Readfile(item,"stock.txt","Name","Item type"," Price ","Available stock");
}
void Add_item_obj(string obj){
int n;
cout<<"Enter no. of "<<obj<<"'s to add: \n";
cin>>n;
item.writeToFile(n,obj);
}
void CD::Add_CD(){
Add_item_obj("CD");
}
void main_menu(){
int i=0;
do{
cout<<"============================\n";
cout<<"-------MAIN MENU------------\n";
cout<<"============================\n";
cout<<"1. Sell Items\n";
cout<<"2. Add Items\n";
cout<<"4. View stock file\n";
cout<<"5. Exit \n";
cout<<"Enter your choice: ";
cin>>i;
switch(i){
case 1: sell_items();
break;
case 2: writetoFile(1,"CD");
break;
case 3: item.ReadFile_item();
break;
default: cout<<"Exiting... \n";
exit(0);
}
}while(i!=0);
}
int main(){
main_menu();
return 0;
}
f.read((char*)&obj, sizeof(obj));
这个演员是无稽之谈。有两种方法可以了解为什么这不可能起作用:
说obj
是一个std::string
。此代码必须读取固定数量的字节(否则,它传递给read
什么?)。但是对于字符串的任何可能内容,多少字节才足够?显然,这不可能奏效。无论传递给 的大小是多少read
,字符串的逻辑值都可以大于此值。
内存的内容可能包含仅在当前进程的上下文中有意义的信息。例如,const char *foo = "hello";
使foo
保持字符串常量的地址"hello"
。将该地址写入文件有什么意义?在此进程运行时,任何后续进程的后续执行将如何不存储在该特定地址中的内容?
您的代码在文件中读取和写入对象的物理内容。您需要读取和写入对象的逻辑内容,而不是这些内容在此过程执行期间在内存中编码的特定方式。
打个比方,我可能会记得某家餐厅是“我二十岁生日时吃饭的地方”。但是,如果我将那家餐厅传达给其他人,告诉他们我记得那家餐厅并没有帮助。我必须以我知道他们会理解的方式对其进行编码。这就是文件格式的作用。
要将数据写入文件,您必须决定文件格式并实现代码以在该格式之间进行转换。对于字符串之类的东西,您要么必须决定您将支持的最大大小并始终读取和写入该字节数,要么使用某种允许可变大小的格式。
使用现有库查看众所周知的文件格式。文本是最简单的,但也有 XML 和 JSON 之类的东西。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句