关于C++文件操作的问题······
//这是Book.h的内容#pragma once
#include <string>
using namespace std;
class Book
{
private:
string number;
string name;
string author;
string press;
string price;
public:
Book();
~Book();
static void addbook();
static void subbook();
static void modbook();
};
//这是Book.cpp的内容(只粘了addbook相关的部分)
void Book::addbook()
{
fstream ofile;
ofile.open("book.txt",ios::app|ios::ate|ios::out);
cout << "请分别输入书的编号、书名、作者、出版社、单价:";
string number, name, author, press;
string price;
cin >> number >> name >> author >> press >> price;
ofile<< number << ',' << name << ',' << author << ',' << press << ',' << price <<'|';
cout << "添加成功!" << endl;
ofile.close();
}
学校实验需要写一个模拟图书管理系统的程序。现在编了一个Book类,想通过addbook()方法向book.txt中写入每本书的相关信息('|'用于分割每本书)。但问题是每次调用Book::addbook()方法都会覆盖掉原本的book.txt,无论原本txt文件里有啥,只要调用了addbook()方法文件里就只剩下我刚刚输入的内容(我明明在打开文件时使用ios::app参数了),而且我刚刚输入的文字还都是在第二行,第一行没有内容,我也不知道这个'\n'是哪来的。自己研究了两天没搞出个所以然只好来问各位大佬了orz······ 把代码贴完整 //这是Book.h的内容
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Book
{
private:
string number;
string name;
string author;
string press;
string price;
public:
Book(){}
~Book(){}
static void addbook();
static void subbook();
static void modbook();
};
//这是Book.cpp的内容(只粘了addbook相关的部分)
void Book::addbook()
{
fstream ofile;
ofile.open("book.txt", ios::app | ios::out);
cout << "请分别输入书的编号、书名、作者、出版社、单价:";
string number, name, author, press;
string price;
cin >> number >> name >> author >> press >> price;
ofile << number << ',' << name << ',' << author << ',' << press << ',' << price << '|';
cout << "添加成功!" << endl;
ofile.close();
}
int main()
{
Book book;
book.addbook();
return 0;
} //Book.h
#pragma once
#include <string>
using namespace std;
class Book
{
private:
string number;
string name;
string author;
string press;
string price;
public:
Book();
~Book();
static void addbook();
static void subbook();
static void modbook();
};
//Book.cpp
#include "Book.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
Book::Book()
{
ofstream ofile;
ofile.open("book.txt");
ofile << '\n';
ofile.close();
}
Book::~Book()
{
}
void Book::addbook()
{
fstream ofile;
ofile.open("book.txt",ios::app|ios::ate|ios::out);
cout << "请分别输入书的编号、书名、作者、出版社、单价:";
string number, name, author, press;
string price;
cin >> number >> name >> author >> press >> price;
ofile<< number << ',' << name << ',' << author << ',' << press << ',' << price <<'|';
cout << "添加成功!" << endl;
ofile.close();
}
void Book::subbook()
{
fstream file;
file.open("book.txt",ios::in);
string tmp , tmp1;
file >> tmp;
cout << tmp;
//file.close();
cout << "请输入待删除图书的书名或编号:";
cin >> tmp1;
int qian, hou, zhong;
zhong = tmp.find(tmp1);
qian = tmp.rfind('|', zhong);
hou = tmp.find('|', zhong);
tmp=tmp.erase(qian + 1, hou);
file.close();
ofstream ofile;
cout << tmp;
ofile.open("book.txt", fstream::trunc);
file << tmp;
cout << "删除成功!" << '\n'; //为什么无法替换file里的内容呢?
}
void Book::modbook()
{
fstream file;
file.open("book.txt", fstream::in);
string tmp, tmp1;
file >> tmp;
cout << "请输入待修改图书的书名或编号:";
cin >> tmp1;
int qian, hou, zhong;
zhong = tmp.find(tmp1);
qian = tmp.rfind('|', zhong);
hou = tmp.find('|', zhong);
string number, name, author, press;
string price;
cout << "请重新输入该图书的信息(按照编号、书名、作者、出版社的顺序):";
cin >> number >> name >> author >> press >> price;
string strtmp = '|' + number + ',' + name + ',' + author + ',' + press + ',' + price + '|';
tmp.replace(qian, hou-1, strtmp);
file.open("book.txt", fstream::out | fstream::trunc);
file << tmp;
cout << "修改成功!" << '\n';
file.close();
}
//源.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "Book.h"
using namespace std;
int main()
{
Book::addbook();
Book::addbook();
Book::subbook();
Book::modbook();
system("pause");
return 0;
}
额,这是所有代码,没有写在同一个cpp里······ 人造人 发表于 2019-3-15 22:00
把代码贴完整
完整代码在下面······ 人造人 发表于 2019-3-15 22:05
哎呀我懂了,是我构造函数写错了{:5_100:}靴靴大佬······
页:
[1]