读写文件出现问题
我把关键的代码贴出来
- C/C++ code
AddnewDlg.cpp// AddnewDlg.cpp : 实现文件//#include "stdafx.h"#include "Bookmanage.h"#include "AddnewDlg.h"#include "afxdialogex.h"#include "BookMessage.h"// CAddnewDlg 对话框IMPLEMENT_DYNAMIC(CAddnewDlg, CDialogEx)CAddnewDlg::CAddnewDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CAddnewDlg::IDD, pParent) , bookid(0) , publishid(0) , authoroneid(0) , authortwoid(0) , authorthreeid(0) , bookname(_T("")) , publishname(_T("")) , authoronename(_T("")) , authortwoname(_T("")) , authorthreename(_T("")){}CAddnewDlg::~CAddnewDlg(){}void CAddnewDlg::DoDataExchange(CDataExchange* pDX){ CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, bookid); DDX_Text(pDX, IDC_EDIT3, publishid); DDX_Text(pDX, IDC_EDIT6, authoroneid); DDX_Text(pDX, IDC_EDIT5, authortwoid); DDX_Text(pDX, IDC_EDIT7, authorthreeid); DDX_Text(pDX, IDC_EDIT2, bookname); DDX_Text(pDX, IDC_EDIT4, publishname); DDX_Text(pDX, IDC_EDIT8, authoronename); DDX_Text(pDX, IDC_EDIT10, authortwoname); DDX_Text(pDX, IDC_EDIT9, authorthreename);}BEGIN_MESSAGE_MAP(CAddnewDlg, CDialogEx) ON_BN_CLICKED(IDC_BUTTON1, &CAddnewDlg::OnBnClickedButton1)END_MESSAGE_MAP()// CAddnewDlg 消息处理程序void CAddnewDlg::OnBnClickedButton1(){ // TODO: 在此添加控件通知处理程序代码 UpdateData(true); Book book;//创建Book对象进行添加 Book temp;//创建空对象写入文本末尾 fstream Hfdat("bookFile.dat",ios::in|ios::out|ios::binary );//以读、写方式打开 Hfdat.seekg(0,ios::beg); do { Hfdat.read((char *)&book,sizeof(Book)); //读一个记录 }while(!endmark(book)); //判断是否结束标志 Hfdat.seekp(-long(sizeof(Book)),ios::cur); //写指针位置 book.bookID=bookid; book.bookName=bookname; book.publishID=publishid; book.publishName=publishname; book.remainder=1; book.total=1; book.author1.authorID=authoroneid; book.author1.authorName=authoronename; if(authortwoid!=0){ book.author2.authorID=authortwoid; book.author2.authorName=authortwoname; } if(authorthreeid!=0){ book.author3.authorID=authorthreeid; book.author3.authorName=authorthreename; } Hfdat.write((char *)&book,sizeof(Book)); Hfdat.write((char *)&temp,sizeof(Book)); Hfdat.close(); MessageBox(L"添加成功",L"提示"); OnOK();}- C/C++ code
AddDlg.cpp// AddDlg.cpp : 实现文件//#include "stdafx.h"#include "Bookmanage.h"#include "AddDlg.h"#include "afxdialogex.h"#include "BookMessage.h"#include "AddnewDlg.h"// CAddDlg 对话框IMPLEMENT_DYNAMIC(CAddDlg, CDialogEx)CAddDlg::CAddDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CAddDlg::IDD, pParent) , bookid(0){}CAddDlg::~CAddDlg(){}void CAddDlg::DoDataExchange(CDataExchange* pDX){ CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, bookid);}BEGIN_MESSAGE_MAP(CAddDlg, CDialogEx) ON_BN_CLICKED(IDC_BUTTON1, &CAddDlg::OnBnClickedButton1)END_MESSAGE_MAP()// CAddDlg 消息处理程序void CAddDlg::OnBnClickedButton1(){ // TODO: 在此添加控件通知处理程序代码 UpdateData(true); fstream Hfdat("bookFile.dat",ios::in|ios::out|ios::binary );//以读、写方式打开 Hfdat.seekg(0,ios::beg); //2进制数据文件读指针移到文件头 Book book; do { Hfdat.read((char *)&book,sizeof(Book)); //读一个数据 }while(book.bookID!=bookid&&!endmark(book)); //判断是否找到记录 if(book.bookID==bookid) { book.remainder++; book.total++; Hfdat.seekp(-long(sizeof(Book)),ios::cur); Hfdat.write((char *)&book,sizeof(Book)); Hfdat.close(); MessageBox(L"已有书籍,增加库存量成功",L"提示"); OnOK(); } else { Hfdat.close(); MessageBox(L"新书籍,请重新填写书籍信息",L"提示"); OnOK(); CAddnewDlg win; win.DoModal(); }}
- C/C++ code
BookMessage.h//书籍类#include "afx.h"#include<fstream>#include<iostream>using namespace std;class Author{public: int authorID;//作者ID号 CString authorName;//作者姓名 };class Book{public: int bookID;//图书号 CString bookName;//图书名 int publishID;//出版社ID CString publishName;//出版社名 int total;//总库存量 int remainder;//现存量 Author author1;//作者一信息 Author author2;//作者二信息 Author author3;//作者三信息 //构造函数 Book(){ bookID=0; bookName=""; publishID=0; publishName=""; total=0; remainder=0; author1.authorID=0; author1.authorName=""; author2.authorID=0; author2.authorName=""; author3.authorID=0; author3.authorName=""; }};class Booklend{public: int bookID;//图书号 int LendID;//借阅者ID int year;//归还期限的年 int month;//归还期限的月 int day;//归还期限的日 Booklend(){ bookID=0; LendID=0; year=0; month=0; day=0; }};int endmark(Book); //判断是否到达文件尾- C/C++ code
//endmark.cpp//判断是否录入空白纪录#include "StdAfx.h"#include"BookMessage.h"int endmark(Book book){ if(book.bookID==0) return 1; return 0;}为什么每次我一添加成功以后好像二进制文件就坏了,等我下一次操作的时候直接程序无法运行下去,在线等,急急急!
[解决办法]
OnBnClickedButton1()中
do
{
Hfdat.read((char *)&book,sizeof(Book)); //读一个记录
}while(!endmark(book)); //判断是否结束标志
Hfdat.seekp(-long(sizeof(Book)),ios::cur); //写指针位置
初始为文件为空,seekp一个负值,会使fstream发生错误吧
[解决办法]
如果用文件流读写类,类中不要用CString保存字符串,改用数组