C++ ios::导致问题的应用程序 #包括 #包括 #包括 #包括 #包括 使用名称空间std; char ch; 课堂用书 { 字符*标题; char*作者; 双倍价格; 整数; 公众: 书() { title=NULL; author=NULL; 价格=0.00; 数量=0; } 无效创建(字符*a、字符*b、双x、整数q) { 标题=新字符; 作者=新字符; strcpy(标题a); strcpy(作者,b); 价格=x; 数量=q; } 无效显示() { 你的代码有很多问题。我在这里要指出的一点是 #include<fstream> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; char ch; class book { char *title; char *author; double price; int quantity; public: book() { title=NULL; author=NULL; price=0.00; quantity=0; } void create(char *a, char *b, double x, int q) { title=new char; author=new char; strcpy(title, a); strcpy(author, b); price=x; quantity=q; } void display() { cout<<"\n"<<title<<"\t"<<author<<"\t"<<price<<"\t"<<quantity; } }; book obj, obj2; fstream stock; void displaystock(); void addbook() { cout << "\033[2J\033[1;1H"; int n, i,q, j; stock.open("stock.txt", ios::app|ios::out|ios::binary); cout<<"\n\nHow many unique book titles would you like to add?"; cin>>n; char *a, *b; double x=0; a=new char; b= new char; for(i=0;i<n;i++) { while ((ch = getchar()) != '\n' && ch != EOF); cout<<stock.tellg(); cout<<"\n\nEnter book title: "; gets(a); cout<<"\n\nEnter author: "; gets(b); cout<<"\n\nEnter price: "; cin>>x; cout<<"\n\nEnter quantity: "; cin>>q; obj.create(a,b,x, q); stock.write((char*)&obj,sizeof(obj)); } stock.close(); } void displaystock() { cout << "\033[2J\033[1;1H"; stock.open("stock.txt", ios::in|ios::binary); cout<<stock.tellg(); while(stock.read((char*)&obj2, sizeof(obj2))) { cout<<"\n"<<stock.tellg(); //if(!stock.eof()) { obj2.display(); } //else //break; } stock.close(); } int main() { addbook(); displaystock(); return 0; }

C++ ios::导致问题的应用程序 #包括 #包括 #包括 #包括 #包括 使用名称空间std; char ch; 课堂用书 { 字符*标题; char*作者; 双倍价格; 整数; 公众: 书() { title=NULL; author=NULL; 价格=0.00; 数量=0; } 无效创建(字符*a、字符*b、双x、整数q) { 标题=新字符; 作者=新字符; strcpy(标题a); strcpy(作者,b); 价格=x; 数量=q; } 无效显示() { 你的代码有很多问题。我在这里要指出的一点是 #include<fstream> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; char ch; class book { char *title; char *author; double price; int quantity; public: book() { title=NULL; author=NULL; price=0.00; quantity=0; } void create(char *a, char *b, double x, int q) { title=new char; author=new char; strcpy(title, a); strcpy(author, b); price=x; quantity=q; } void display() { cout<<"\n"<<title<<"\t"<<author<<"\t"<<price<<"\t"<<quantity; } }; book obj, obj2; fstream stock; void displaystock(); void addbook() { cout << "\033[2J\033[1;1H"; int n, i,q, j; stock.open("stock.txt", ios::app|ios::out|ios::binary); cout<<"\n\nHow many unique book titles would you like to add?"; cin>>n; char *a, *b; double x=0; a=new char; b= new char; for(i=0;i<n;i++) { while ((ch = getchar()) != '\n' && ch != EOF); cout<<stock.tellg(); cout<<"\n\nEnter book title: "; gets(a); cout<<"\n\nEnter author: "; gets(b); cout<<"\n\nEnter price: "; cin>>x; cout<<"\n\nEnter quantity: "; cin>>q; obj.create(a,b,x, q); stock.write((char*)&obj,sizeof(obj)); } stock.close(); } void displaystock() { cout << "\033[2J\033[1;1H"; stock.open("stock.txt", ios::in|ios::binary); cout<<stock.tellg(); while(stock.read((char*)&obj2, sizeof(obj2))) { cout<<"\n"<<stock.tellg(); //if(!stock.eof()) { obj2.display(); } //else //break; } stock.close(); } int main() { addbook(); displaystock(); return 0; },c++,file,binary,C++,File,Binary,您将title和author声明为char*类型,并使用它们从输入参数a和b复制数据。您只为以下中的1个char分配内存 void create(char *a, char *b, double x, int q) { title=new char; author=new char; strcpy(title, a); strcpy(author, b); price=x; quantity=q; } 很明显,在strcpy时,您

您将
title
author
声明为
char*
类型,并使用它们从输入参数a和b复制数据。您只为以下中的1个char分配内存

 void create(char *a, char *b, double x, int q)
 {
     title=new char;
     author=new char;
     strcpy(title, a);
     strcpy(author, b);
     price=x;
     quantity=q;
}
很明显,在strcpy时,您打算将其视为一个数组。结果是,您很可能正在写入未分配的内存,因为您没有提供足够的空间将数据复制到其中。为此,您最好使用
std::string
。此外,您还有许多glob所有变量都散落在你的代码中

 title = new char;
不好。将类的实例地址转换为
char*
,真的有意义吗?我会将代码分解为更简单的部分,停止使用这么多全局变量(考虑改用函数参数传递东西)并尝试通过使用调试器逐步检查代码来识别特定问题。希望这将帮助您识别和修复一些问题


最后一点-您无法删除使用
new

ios::app
分配的任何内存,这几乎肯定不是问题所在。您的代码存在严重问题(正如其他人所回答的),需要解决。
  stock.write((char*)&obj,sizeof(obj));