鱼C论坛

 找回密码
 立即注册
查看: 2563|回复: 5

关于C++文件操作的问题······

[复制链接]
发表于 2019-3-15 19:52:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//这是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······
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-15 22:00:04 | 显示全部楼层
把代码贴完整
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-15 22:05:30 | 显示全部楼层
//这是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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-15 22:45:46 | 显示全部楼层
//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里······
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-15 22:47:02 | 显示全部楼层

完整代码在下面······
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-15 22:56:13 | 显示全部楼层

哎呀我懂了,是我构造函数写错了靴靴大佬······
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 12:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表