我是新手,尝试写入文本文件时出现以下错误。似乎问题出在文本文件关闭时。
Ammar_1610852_Assignment.exe中0x6b20d0ac的未处理异常:
0xC0000005:访问冲突读取位置0x00003232。
#pragma once
#include "stdafx.h"
#include "targetver.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
void cust_info();
void bookshop_stack();
void search();
void insert();
void list();
void push();
void display();
void pop();
void main_menu();
void main()
{
main_menu();
getch();
}
void main_menu()
{
int opts;
//this is welcome message of the app
printf ("---- Welcome to Ammar's Book Shop----\n\n\n\nPlease Select an Option From Below\n\n 1. Customer Information\n 2. Book Shop Stack \n 3. Exit\n");
scanf("%d",&opts);
//this is the main three options in the app
switch (opts)
{
case 1:
cust_info();
break;
case 2:
bookshop_stack();
break;
case 3:
printf ("Exit");
break;
default:
break;
}
}
void cust_info()
{
int sub2_opts;
printf ("Please Select an Option From Below\n\n 1. Insert \n 2. Search \n 3. List \n 4. Exit \n\n");
scanf ("%d",&sub2_opts);
switch (sub2_opts)
{
case 1:
insert();
break;
case 2:
search();
break;
case 3:
list();
break;
case 4:
main_menu();
break;
default:
printf ("Please select a valid option\n");
break;
}
}
void bookshop_stack()
{
int bs_opts;
printf ("Please Select an Option From Below\n\n 1. Push \n 2. Search \n 3. Display \n 4. Exit \n\n");
scanf ("%d",&bs_opts);
switch (bs_opts)
{
case 1:
push();
break;
case 2:
display();
break;
case 3:
pop();
break;
case 4:
main_menu();
break;
default:
printf ("Please select a valid option\n");
break;
}
}
void search()
{
printf ("hello this is search screen");
}
void list()
{
char buf[256];
std::ifstream inFile("bookshop_DB.txt");
if (!inFile.is_open()) {
std::cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> buf) {
std::cout << buf << std::endl;
}
inFile.close();
}
void push()
{
}
void display()
{
}
void pop()
{
}
void insert()
{
int phon,bsn,dob,id,dop,dor,fa,opsn;
char name[30],adds[30],bt[50],ban[50];
FILE * bookdetails;
bookdetails = fopen ("bookshop_DB.txt","a");
printf ("Enter Customer Name\n");
scanf("%s",name);
printf ("Enter Customer ID\n");
scanf ("%d",& id);
printf ("Enter Customer Address\n");
scanf ("%s",adds);
printf ("Enter Customer Phone Number\n");
scanf ("%d",&phon);
printf ("Enter Book Serial Number\n");
scanf ("%s",&bsn);
printf ("Enter Book Title\n");
scanf ("%s",bt);
printf ("Enter Book Author Name\n");
scanf ("%s",ban);
printf ("Enter Date of Borrow\n");
scanf ("%s",&dob);
printf ("Enter Date of Publish\n");
scanf ("%s",&dop);
printf ("Enter Date of Return\n");
scanf ("%s",&dor);
printf ("Enter Fine Amount\n");
scanf ("%s",&fa);
fprintf (bookdetails,"%s %d %s %d %s %s %s %s %s %s %s", name,id,adds,phon,bsn,bt,ban,dob,dop,dor,fa);
fclose (bookdetails);
printf ("Deatils have been saved successfully..!!\n\n Press 1 to Main Menu or 0 to Exit\n");
scanf ("%d",&opsn);
switch (opsn)
{
case 1:
cust_info();
break;
case 0:
break;
default:
printf ("Please select a valid option\n");
break;
}
}
最佳答案
您的问题是scanf
和fprintf
都使用了错误的格式说明符。
一个例子:
printf ("Enter Fine Amount\n");
scanf ("%s",&fa);
变量
fa
是整数,但是您尝试使用%s
进行扫描。对于fprintf
,您尝试使用%s
打印整数时也是如此。对字符串使用%s
。对于整数,请使用%d
。变量
fa
只是一个示例。还有更多...(例如dor
)-因此,请检查所有格式说明符。顺便说一句,当您想回到
main_menu
时,请不要拨打它。只需使用退货。所以代替:case 4:
main_menu();
break;
只需做:
case 4:
return;
关于c - Ammar_1610852_Assignment.exe中0x6b20d0ac处未处理的异常:0xC0000005:访问冲突读取位置0x00003232,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44216041/