c++ - 再次打开程序后文件中的数据自动删除
问题描述
好的,我已经知道混合 C 和 C++ 是禁忌,但目前这是我目前所知道的,因为我们的教授教我们略差。考虑到这一点,我也在尝试通过阅读 Bjarne Stroustrup 的书“使用 C++ 编程原理和实践”自学。显然,我们的教授告诉我们用这些细节创建一个 POS 系统;
POS(销售点)
1.)Allow multiple transaction(Multiple orders)
2.)Payment
3.)Receipt Generation
4.)Update Product Record
5.)View Product list
6.)All transaction should be saved
问题是每当我执行该程序时,将一些数据放入文件中并退出程序。每当我再次执行程序时,文件中的数据会自动删除或重新加载。我看不到问题的原样。请帮忙!:< PS:你可以批评我,因为它使编写和编程代码更有动力。
这是代码:
更新:
好的,所以我尝试使用正确的功能fstream
,但仍然出现错误。也许是因为指针。
代码:
#include<iostream>
#include<fstream>
using namespace std;
//=============================================================================//
char decide;
int menu,loop = 0,dCheck;
float quan,total = 0.00,sbTotal,scbTotal,scbdTotal,sqpTotal,bsTotal,bsdTotal,ssTotal,sssTotal,fsTotal,mTotal,change,tChange;
double sb = 3.00,scb = 3.50,scbd = 4.50,sqp = 6.50,bs = 8.00,bsd = 9.00,ss = 3.00,sss = 3.25,fs = 2.50,m = 2.00;
//=============================================================================//
struct data{
/*For the append file*/
int temp_quan,temp_sbTotal,temp_scbTotal,temp_scbdTotal,temp_sqpTotal,temp_bsTotal,temp_bsdTotal,temp_ssTotal,temp_sssTotal,temp_fsTotal,temp_mTotal;
/*For the temp file*/
int t_quan,t_sbTotal,t_scbTotal,t_scbdTotal,t_sqpTotal,t_bsTotal,t_bsdTotal,t_ssTotal,t_sssTotal,t_fsTotal,t_mTotal;
data *next;
};
int decideCheck(char a){
if(decide == 'y' || decide == 'Y'){
return 1;
}
else if(decide == 'N' || decide == 'n'){
return 0;
}
}
int main(){
data *a,*b;
a = new data;
b = new data;
fstream *record;
fstream *tempRecord;
while(1){
while(1){/*MAIN MENU*/
record.open("Sale Record",ios::app);
tempRecord.open("temp Sale Record",ios::out);
cout << "----------------------------------------------------------------\n\t\t\tSally's Burgers\n----------------------------------------------------------------\n";
cout << "\t\t ***** Main Menu *****\n================================================================\n";
cout << "\t\tItem\t\t\t\tPrice\n\t<1>Sally's Burger\t\t\t$3.00\n\t<2>Sally's CheeseBurger\t\t\t$3.50\n\t<3>Sally's CheeseBurger Deluxe\t\t$4.50\n\t<4>Sally's QuarterPounder\t\t$6.50\n\t<5>BIG Sally\t\t\t\t$8.00\n\t<6>BIG Sally Deluxe\t\t\t$9.50\n\t<7>Sally's Salad\t\t\t$3.00\n\t<8>Sally's Salad with sides\t\t$3.25\n\t<9>Frost Shake\t\t\t\t$2.50\n\t<10>Milkshakes\t\t\t\t$2.00\n\n\t<0>End Order\n";
cout << "================================================================\n";
cout << "\t\t\t\t\tTotal Amount[ $" << total << " ]";
cout << "\n\t\t ***** Your Order *****\n\n\t>>Item Number: ";
cin >> menu;
if(menu == 0){
break;
}
switch(menu){
case 1:{
cout << "\t>>Item Name <Sally's Burger> |$3.00 each|\n\t>>Quantity: ";
cin >> quan;
a -> temp_quan = quan;
a -> t_quan = quan;
sbTotal = quan * sb;
a -> temp_sbTotal = sbTotal;
a -> t_sbTotal = sbTotal;
total = sbTotal + total;
record << a -> temp_quan << " Sally's Burger\t\t\t\t$3.00\t$ " << a -> temp_sbTotal;
record.close();
tempRecord << a -> t_quan << " Sally's Burger\t\t\t\t$3.00\t$ " << a -> t_sbTotal;
tempRecord.close();
system("cls");
break;
}
解决方案
正如我们所见,在您的 main() 中:
int main(){
data *a;
a = new data;
FILE *record;
> record = fopen("Sale Record","w");
> fclose(record);
if(record = fopen("Sale Record","r")){
fclose(record);
while(1){
record = fopen("Sale Record","w");
如果找不到,则删除整个文件或创建一个新文件。
它看起来是故意的fclose(record);
。
record = fopen("Sale Record","r");
以只读模式打开文件。
如果你想写在文件的末尾,你应该使用
record = fopen("Sale Record","a");
如果你想做其他操作,你可以继续fopen - C++ 参考。
另外,我宁愿建议您使用fstream而不是stdio.h,有关更多详细信息,您可以继续Input/output with files - C++ reference。
推荐阅读
- sql-server - 并行执行 SQL 命令的选项
- javascript - JavaScript 先将 markdown 拆分为标题,然后拆分为句子
- javascript - parceljs bundler 无法处理 javascript 模板字符串中的 svg src
- pdf - 从头生成PDF,字形如何映射到字符代码?
- angular - 如何在 Angular 中测试条件?
- ruby-on-rails - 如果在rails参数中传递%,如何防止参数引发错误
- flutter - .documentID 未返回正确信息
- erlang - 如何在 Erlang 中打印一个空字符串
- django - 如何从 html 表单中保存外文(选择)?
- latex - 为什么引用自制环境时区块头的高度会增加?如何避免这种情况?