鱼C论坛

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

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

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

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

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

x
  1. //这是Book.h的内容
  2. #pragma once
  3. #include <string>
  4. using namespace std;
  5. class Book
  6. {
  7. private:
  8.         string number;
  9.         string name;
  10.         string author;
  11.         string press;
  12.         string price;
  13. public:
  14.         Book();
  15.         ~Book();
  16.         static void addbook();
  17.         static void subbook();
  18.         static void modbook();
  19. };

  20. //这是Book.cpp的内容(只粘了addbook相关的部分)
  21. void Book::addbook()
  22. {
  23.         fstream ofile;
  24.         ofile.open("book.txt",ios::app|ios::ate|ios::out);
  25.         cout << "请分别输入书的编号、书名、作者、出版社、单价:";
  26.         string number, name, author, press;
  27.         string price;
  28.         cin >> number >> name >> author >> press >> price;
  29.         ofile<< number << ',' << name << ',' << author << ',' << press << ',' << price <<'|';
  30.         cout << "添加成功!" << endl;
  31.         ofile.close();
  32. }
复制代码

学校实验需要写一个模拟图书管理系统的程序。现在编了一个Book类,想通过addbook()方法向book.txt中写入每本书的相关信息('|'用于分割每本书)。但问题是每次调用Book::addbook()方法都会覆盖掉原本的book.txt,无论原本txt文件里有啥,只要调用了addbook()方法文件里就只剩下我刚刚输入的内容(我明明在打开文件时使用ios::app参数了),而且我刚刚输入的文字还都是在第二行,第一行没有内容,我也不知道这个'\n'是哪来的。自己研究了两天没搞出个所以然只好来问各位大佬了orz······
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-15 22:00:04 | 显示全部楼层
把代码贴完整
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-15 22:05:30 | 显示全部楼层
  1. //这是Book.h的内容

  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6. class Book
  7. {
  8. private:
  9.         string number;
  10.         string name;
  11.         string author;
  12.         string press;
  13.         string price;
  14. public:
  15.         Book(){}
  16.         ~Book(){}
  17.         static void addbook();
  18.         static void subbook();
  19.         static void modbook();
  20. };

  21. //这是Book.cpp的内容(只粘了addbook相关的部分)
  22. void Book::addbook()
  23. {
  24.         fstream ofile;
  25.         ofile.open("book.txt", ios::app | ios::out);
  26.         cout << "请分别输入书的编号、书名、作者、出版社、单价:";
  27.         string number, name, author, press;
  28.         string price;
  29.         cin >> number >> name >> author >> press >> price;
  30.         ofile << number << ',' << name << ',' << author << ',' << press << ',' << price << '|';
  31.         cout << "添加成功!" << endl;
  32.         ofile.close();
  33. }

  34. int main()
  35. {
  36.         Book book;
  37.         book.addbook();
  38.         return 0;
  39. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-15 22:45:46 | 显示全部楼层
  1. //Book.h
  2. #pragma once
  3. #include <string>
  4. using namespace std;
  5. class Book
  6. {
  7. private:
  8.         string number;
  9.         string name;
  10.         string author;
  11.         string press;
  12.         string price;
  13. public:
  14.         Book();
  15.         ~Book();
  16.         static void addbook();
  17.         static void subbook();
  18.         static void modbook();
  19. };

  20. //Book.cpp
  21. #include "Book.h"
  22. #include <iostream>
  23. #include <fstream>
  24. #include <string>
  25. using namespace std;

  26. Book::Book()
  27. {
  28.         ofstream ofile;
  29.         ofile.open("book.txt");
  30.         ofile << '\n';
  31.         ofile.close();
  32. }


  33. Book::~Book()
  34. {
  35. }


  36. void Book::addbook()
  37. {
  38.         fstream ofile;
  39.         ofile.open("book.txt",ios::app|ios::ate|ios::out);
  40.         cout << "请分别输入书的编号、书名、作者、出版社、单价:";
  41.         string number, name, author, press;
  42.         string price;
  43.         cin >> number >> name >> author >> press >> price;
  44.         ofile<< number << ',' << name << ',' << author << ',' << press << ',' << price <<'|';
  45.         cout << "添加成功!" << endl;
  46.         ofile.close();
  47. }


  48. void Book::subbook()
  49. {
  50.         fstream file;
  51.         file.open("book.txt",ios::in);
  52.         string tmp , tmp1;
  53.         file >> tmp;
  54.         cout << tmp;
  55.         //file.close();
  56.         cout << "请输入待删除图书的书名或编号:";
  57.         cin >> tmp1;
  58.         int qian, hou, zhong;
  59.         zhong = tmp.find(tmp1);
  60.         qian = tmp.rfind('|', zhong);
  61.         hou = tmp.find('|', zhong);
  62.         tmp=tmp.erase(qian + 1, hou);
  63.         file.close();
  64.         ofstream ofile;
  65.         cout << tmp;
  66.         ofile.open("book.txt", fstream::trunc);
  67.         file << tmp;
  68.         cout << "删除成功!" << '\n';                                //为什么无法替换file里的内容呢?
  69. }


  70. void Book::modbook()
  71. {
  72.         fstream file;
  73.         file.open("book.txt", fstream::in);
  74.         string tmp, tmp1;
  75.         file >> tmp;
  76.         cout << "请输入待修改图书的书名或编号:";
  77.         cin >> tmp1;
  78.         int qian, hou, zhong;
  79.         zhong = tmp.find(tmp1);
  80.         qian = tmp.rfind('|', zhong);
  81.         hou = tmp.find('|', zhong);
  82.         string number, name, author, press;
  83.         string price;
  84.         cout << "请重新输入该图书的信息(按照编号、书名、作者、出版社的顺序):";
  85.         cin >> number >> name >> author >> press >> price;
  86.         string strtmp = '|' + number + ',' + name + ',' + author + ',' + press + ',' + price + '|';
  87.         tmp.replace(qian, hou-1, strtmp);
  88.         file.open("book.txt", fstream::out | fstream::trunc);
  89.         file << tmp;
  90.         cout << "修改成功!" << '\n';                               
  91.         file.close();
  92. }

  93. //源.cpp
  94. #include <iostream>
  95. #include <string>
  96. #include <fstream>
  97. #include <iomanip>
  98. #include "Book.h"
  99. using namespace std;
  100. int main()
  101. {
  102.         Book::addbook();
  103.         Book::addbook();
  104.         Book::subbook();
  105.         Book::modbook();
  106.         system("pause");
  107.         return 0;
  108. }
复制代码


额,这是所有代码,没有写在同一个cpp里······
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

完整代码在下面······
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

哎呀我懂了,是我构造函数写错了靴靴大佬······
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 18:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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