Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1390253
  • 博文数量: 120
  • 博客积分: 182
  • 博客等级: 入伍新兵
  • 技术积分: 2278
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-19 16:31
文章分类

全部博文(120)

文章存档

2015年(12)

2014年(13)

2013年(40)

2012年(55)

分类: C/C++

2013-10-30 17:24:36

最近,因为项目的需求,需要自己编写关于txt格式文件操作的接口。要求是打开文件时,如果没有该文件,就自行创建文件,文件的名字是由传递的参数决定的。写文件是以追加的方式写入。清除操作完成后文件必须是处于清除之前的状态(打开状态)。写完后,写了测试代码做了测试,暂时没发现啥问题,记录下来,以备以后使用或者改进。

点击(此处)折叠或打开

  1. //storageinterface.h
  2. #ifndef STORAGEINTERFACE_H
  3. #define STORAGEINTERFACE_H

  4. namespace XUPT {
  5. class StorageInterface;
  6. }

  7. #include <QString>

  8. template <typename T>
  9. class StorageInterface
  10. {
  11. public:
  12.     virtual bool open() = 0;
  13.     virtual T read() = 0;
  14.     virtual void write(const T& info) = 0;
  15.     virtual void close() = 0;
  16.     virtual void clear() = 0;
  17. };

  18. #endif // STORAGEINTERFACE_H

点击(此处)折叠或打开

  1. //counterfile.h
  2. #ifndef COUNTERFILE_H
  3. #define COUNTERFILE_H

  4. #include "storageinterface.h"
  5. #include <QString>
  6. #include <QFile>
  7. #include <QTextStream>

  8. //#include <iostream>
  9. using namespace std;

  10. class CounterFile : public StorageInterface<QString>
  11. {
  12. public:
  13.     CounterFile(int unitId, int shift);
  14.     ~CounterFile();
  15.     virtual bool open();
  16.     // 每次返回一组counter值(counterId:counter),文件结束返回""
  17.     // 例如 "3:120" 表示ID为3,counter为120
  18.     virtual QString read();
  19.     // 每次写入的info格式为(counterId:counter)
  20.     virtual void write(const QString& info);
  21.     virtual void close();
  22.     virtual void clear();
  23. private:
  24.     QString path;
  25.     QString file_path;
  26.     QByteArray line;
  27.     //QString str_line;
  28.     QFile *pfile;
  29.     QTextStream *in;
  30.     QDataStream *clearin;
  31.     string counter_id;
  32.     string counters;
  33.     int size;
  34. };

  35. #endif // COUNTERFILE_H

点击(此处)折叠或打开

  1. //counterfile.cpp
  2. #include "counterfile.h"
  3. #include <QDebug>
  4. #include <QStringList>

  5. CounterFile::CounterFile(int unitId, int shift)
  6. {
  7.     path = unitId + 48; // to ascii: 1 -> "1"
  8.     path += shift + 48;
  9.     path += "_c"; // exp:"01_c" 表示机组为C0,班次为1的Counter统计文件
  10. }

  11. CounterFile::~CounterFile()
  12. {
  13. }

  14. bool CounterFile::open()
  15. {

  16.     file_path = path + ".txt";
  17.     pfile = new QFile(file_path);

  18.     if (pfile->open(QIODevice::ReadWrite | QIODevice::Text)) {
  19.         qDebug() << "open ok!";
  20.         return true;
  21.     } else if (!pfile->open(QIODevice::WriteOnly)) {//如果文件不存在就创建
  22.         pfile->close();
  23.         if (pfile->open(QIODevice::ReadWrite | QIODevice::Text)) {
  24.             qDebug() << "open ok!";
  25.             return true;
  26.         } else {
  27.             qDebug() << "open wrong!";
  28.             return false;
  29.         }
  30.     } else
  31.         return false;
  32. }

  33. QString CounterFile::read()
  34. {

  35.     if (pfile->atEnd()) {
  36.         qDebug() << "read1";
  37.         return "";
  38.     } else {
  39.             qDebug() << "read2";
  40.             line = pfile->readLine();
  41.             return line;
  42.         }
  43. }

  44. void CounterFile::write(const QString &info)
  45. {
  46.     in = new QTextStream(pfile);
  47.     in->seek(pfile->size());//指针移到文件尾,保证以追加的方式写入
  48.     *in << info << '\n';
  49.     delete in;
  50.     qDebug() << "write test3";
  51. }

  52. void CounterFile::close()
  53. {
  54.     qDebug() << "close1";
  55.     pfile->close();
  56.     qDebug() << "close2";
  57.     delete pfile;
  58.     qDebug() << "close";
  59. }

  60. void CounterFile::clear()
  61. {
  62.    pfile->remove();
  63.    pfile->open(QIODevice::WriteOnly);
  64.    pfile->close();
  65.    if (pfile->open(QIODevice::ReadWrite | QIODevice::Text)) {
  66.        return ;
  67.    }
  68. }

点击(此处)折叠或打开(测试。。。。。。)

  1. //main.cpp
  2. #include "mainwindow.h"
  3. #include "counterfile.h"
  4. #include <QApplication>
  5. #include <QDebug>

  6. int main(int argc, char *argv[])
  7. {
  8. // QApplication a(argc, argv);
  9. // MainWindow w;

  10.     CounterFile cntfile(3, 5);
  11.     cntfile.open();
  12.     cntfile.write("2:23");
  13.     cntfile.write("2:23");
  14.     cntfile.write("2:23");
  15.     cntfile.write("2:23");

  16. // qDebug() << cntfile.read();
  17. // qDebug() << cntfile.read();
  18. // qDebug() << cntfile.read();
  19. // qDebug() << cntfile.read();
  20.     //cntfile.close();

  21.     //cntfile.open();
  22.     qDebug() << cntfile.read();
  23.     qDebug() << cntfile.read();
  24.     qDebug() << cntfile.read();
  25.     qDebug() << cntfile.read();
  26.     qDebug() << cntfile.read();
  27.     qDebug() << cntfile.read();
  28.     //cntfile.close();

  29.     qDebug() << "-------------------1";

  30. // qDebug() << cntfile.read();
  31. // qDebug() << cntfile.read();
  32. // qDebug() << cntfile.read();
  33. // qDebug() << cntfile.read();

  34.    //CounterFile cntfile1(5, 6);
  35.     //cntfile.open();
  36.     cntfile.write("2:23");
  37.     cntfile.write("2:23");
  38.     cntfile.write("2:23");
  39.     cntfile.write("2:23");
  40. // qDebug() << cntfile1.read();
  41. // qDebug() << cntfile1.read();
  42. // qDebug() << cntfile1.read();
  43. // qDebug() << cntfile1.read();
  44. // qDebug() << cntfile1.read();
  45. // qDebug() << cntfile1.read();
  46.     cntfile.close();

  47.     qDebug() << "-------------file2";

  48.     cntfile.open();
  49. // cntfile1.write("2:23");
  50. // cntfile1.write("2:23");
  51. // cntfile1.write("2:23");
  52. // cntfile1.write("2:23");

  53. // qDebug() << cntfile.read();
  54. // qDebug() << cntfile.read();
  55. // qDebug() << cntfile.read();
  56. // qDebug() << cntfile.read();


  57.     //cntfile.clear();
  58.     qDebug() << cntfile.read();
  59.     qDebug() << cntfile.read();
  60.     qDebug() << cntfile.read();
  61.     qDebug() << cntfile.read();
  62.     qDebug() << cntfile.read();
  63.     qDebug() << cntfile.read();
  64.     cntfile.close();

  65.     qDebug() << "-------------------5";
  66.     return 0;
  67. }



阅读(11852) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~