運用fstream對文本文件進行指定讀取、刪除,指定位置添加一行操作類

1、頭文件

#ifndef FILEOPERATION_H
#define FILEOPERATION_H

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class FileOperation
{
public:
 FileOperation();
 FileOperation(char* );
 ~FileOperation();
 bool Open(char*);
 string ReadLine(int index = 1);
 bool WriteLine(char* , int index = 1);
 bool RemoveLine(int index = 1);
 void Close();
private:
 string m_fName;
 fstream m_file;
};

#endif

 

2、實現文件

#include "FileOperation.h"

FileOperation::FileOperation()
{
}

FileOperation::FileOperation(char* fName) : m_fName(fName)
{
}

FileOperation::~FileOperation()
{

}

bool FileOperation::Open(char* fName)
{
 if(m_file.is_open())
 {
  m_file.close();
  m_file.clear();
 }
 m_file.open(fName,ios::in | ios::out | ios::binary);
 m_fName = fName;
 if(m_file.is_open())
 {
  return true;
 }
 else
 {
  return false;
 }
}


string FileOperation::ReadLine(int index)
{
 if(!m_file.is_open())
 {
  m_file.open(m_fName.c_str(), ios::in | ios::out | ios::binary);
 }
 else
 {
  m_file.seekg(0,ios::beg);
 }
 string s;
 int icount = 0 ;
 while(getline(m_file, s))
 {
  icount++;
  if(icount == index)
  {
   break;
  }
 }
 return s;
}

bool FileOperation::WriteLine(char* sst, int index)
{
 if(!m_file.is_open())
 {
  m_file.open(m_fName.c_str(), ios::in | ios::out | ios::binary);
 }
 else
 {
  m_file.clear();
  m_file.seekg(0,ios::beg);
 }
 
 ofstream oftmp;
 oftmp.open("tmp.tmp",ios::out | ios::binary | ios::trunc);
 if(!oftmp.is_open())
 {
  return false;
 }
 
 string s;
 int icount = 0;
 while(getline(m_file,s))
 {
  icount++;
  if(icount == index)
  {
   oftmp << sst << "/r/n"; //.write(sst,strlen(sst));
  }
  oftmp << s << endl; //.write(s.c_str(),strlen(s.c_str()));
 }
 oftmp.close();
 oftmp.clear();
 m_file.close();
 m_file.clear();
 
 oftmp.open(m_fName.c_str(),ios::out | ios::trunc | ios::binary);
 if(!oftmp.is_open())
 {
  return false;
 }
 ifstream iftmp;
 iftmp.open("tmp.tmp",ios::in | ios::binary);
 if(!iftmp.is_open())
 {
  return false;
 }
 while(getline(iftmp,s))
 {
  oftmp << s << endl;
 }
 
 iftmp.close();
 iftmp.clear();
 oftmp.close();
 oftmp.clear();
 remove("tmp.tmp");

 m_file.open(m_fName.c_str(), ios::in | ios::out | ios::binary);

 return true;
}

bool FileOperation::RemoveLine(int index)
{
 if(!m_file.is_open())
 {
  m_file.open(m_fName.c_str(), ios::in | ios::out | ios::binary);
 }
 else
 {
  //vm_file.clear();
  m_file.seekg(0,ios::beg);
  
 }
 int icount = 0;
 string s;
 ofstream ftmp("tmp.tmp", ios::out | ios::binary);
 if(!ftmp)
 {
  return false;
 }
 while(getline(m_file,s))
 {
  icount++;
  if(icount == index)
  {
   continue;
  }
  else
  {
   ftmp << s << endl;
  }
 }
 ftmp.close();
 ftmp.clear();
 m_file.close();
 m_file.clear();

 m_file.open(m_fName.c_str(),ios::out | ios::trunc | ios::binary);
 if(!m_file.is_open())
 {
  return false;
 }
 ifstream iftmp;
 iftmp.open("tmp.tmp", ios::in | ios::binary);
 while(getline(iftmp,s))
 {
  m_file << s << endl;
 }
 ftmp.close();
 ftmp.clear();
 m_file.close();
 m_file.clear();
 remove("tmp.tmp");

 m_file.open(m_fName.c_str(), ios::in | ios::out | ios::binary);
 return true;
}

void FileOperation::Close()
{
 if(m_file.is_open())
 {
  m_file.close();
  m_file.clear();
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章