美文网首页
文件读写--代码

文件读写--代码

作者: 7bfedbe4863a | 来源:发表于2018-01-14 00:44 被阅读0次

在写代码之前,我大概想了一下,文件操作大多是文件的读写操作,所以就先把最基本的读写操作的部分写了一下,包括:

  • File(char* strFilePath); //类的构造函数,其实也就是把传过来的(路径和)参数名赋值给类的成员变量m_fp,方便后面函数在使用的时候不需要添加文件的(路径和)文件名。
  • bool ReadFile(string& pResult); //把文件中的内容读取到pResult里,相应的文件要存在,不存在就返回false。在写这个函数的时候,调用fgets函数一直失败(但是fputs就可以很愉快的使用),调用中断,所以退而求其次选了fgtec函数(使用这个就要把字符往字符串添加,有点麻烦)。
  • bool WriteToFile(char* pData); //如果有路径和文件名对应的文件,就打开,没有就创建;先把文件清空,再把pData的内容写到文件里。(这次写的代码中,向文件中添加的数据类型被我写了固定的,下次试一下写其他的。)
  • bool AddToFile(char* pData); //和上面的那个类似,不过这个不清空原文件的内容,只在文件的结尾处添加pData的内容。

这些函数调用的耍函数有:

  • bool_CheckFileIsOpen();//判断文件是不是已经打开了,这个在对文件的不同操作的时候很重要,毕竟不同的操作对于文件打开的方式要求也不同
  • bool_OpenFile(char*mode);//这个函数根据参数的不同,对文件进行不同的打开文件操作。参数是打开文件的方式,具体的参数可以传递的内容可以看前一篇理论里的说明。

之后对这个类进行了一些功能的扩展,来进行一些其他的处理:

  • int GetFileRaw(); //判断文件里面有几行
  • vecPos MatchToFile(string& pData);//用来对文件里的内容和参数进行匹配,返回匹配成功的每个字符串所在的位置的vector。

下面是这些函数调用的其他函数的说明:

  • bool _Match(string& src,string& res);//把两个同样长度的字符串进行比较
  • bool _GetMatchDataFromFile(int nPos,string& src,int nLength);//从文件中获取和要匹配的字符串同样长度的字符串
  • void _ToMatch(int nPos,string& data,Pos dataPos,vecPos& vecResult);//开始匹配操作
File.h
#pragma once
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

struct Pos{
public:
    int raw;
    int col;
    Pos(){
        raw = 0;
        col = 0;
    }
    Pos(int r,int c)
    {
        raw = r;
        col = c;
    }
};

typedef vector<Pos> vecPos;

class File
{
public:
    File(void);
    ~File(void);
    File(char* strFilePath);
    bool ReadFile(string& pResult);
    bool WriteToFile(char* pData);
    bool AddToFile(char* pData);
    int GetFileRaw();
    vecPos MatchToFile(string& pData);
protected:
    bool _CheckFileIsOpen();
    bool _OpenFile(char* mode);
    bool _Match(string& src,string& res);
    bool _GetMatchDataFromFile(int nPos,string& src,int nLength);
    void _ToMatch(int nPos,string& data,Pos dataPos,vecPos& vecResult);
private:
    FILE* m_fp;
    char* m_strFilePath;
};
File.cpp
#include "StdAfx.h"
#include "File.h"

File::File(void)
{
}

File::File(char* strFilePath)
{
    m_fp = NULL;
    m_strFilePath = strFilePath;
}

File::~File(void)
{
    if (_CheckFileIsOpen())
    {
        fclose(m_fp);
    }
    m_strFilePath = NULL;
    delete m_strFilePath;
}

bool File::ReadFile(string& pResult)
{
    if(_OpenFile("r+"))
    {
        //char* str = "";
        pResult = "";
        char temp[2];
        fseek(m_fp,0,SEEK_SET);
        char ch;
        while(!feof(m_fp))
        {
            //fgets(str,512,m_fp);      //这一句代码有问题
            ch=fgetc(m_fp);
            temp[0] = ch;
            temp[1] = '\0';
            pResult.append(temp);
        }
        return true;
    }
    return false;
}

bool File::_CheckFileIsOpen()
{
    if (m_fp == NULL)
    {
        return false;
    }
    return true;
}

bool File::_OpenFile(char* mode)
{
    if (_CheckFileIsOpen())
    {
        fclose(m_fp);
    }
    m_fp = fopen(m_strFilePath,mode);
    if (m_fp == NULL)
    {
        return false;
    }
    return true;
}

bool File::WriteToFile(char* pData)
{
    if (_OpenFile("w+"))
    {
        fputs(pData,m_fp);
        return true;
    }
    return false;
}

bool File::AddToFile(char* pData)
{
    if (_OpenFile("a+"))
    {
        fputs(pData,m_fp);
        return true;
    }
    return false;
}

int File::GetFileRaw()
{
    if (_OpenFile("r+"))
    {
        int nRawCount = 1;
        fseek(m_fp,0,SEEK_SET);
        char ch;
        while(!feof(m_fp))
        {
            ch=fgetc(m_fp);
            if (ch == '\n')
            {
                nRawCount++;
            }
        }
        return nRawCount;
    }
    return -1;
}

vecPos File::MatchToFile(string& pData)
{
    vecPos vecResult;
    int tempcol = 0;
    int offset = 0;
    if (_OpenFile("r+"))
    {
        Pos tempPos;
        char temp[2];
        fseek(m_fp,0,SEEK_SET);
        while(!feof(m_fp))
        {
            char ch=fgetc(m_fp);
            if (ch == pData[0])
            {
                _ToMatch(offset,pData,tempPos,vecResult);
            }
            if (ch == '\n')
            {
                tempPos.raw++;
                tempPos.col = -1;
                offset++;
                continue;
            }
            offset++;
            tempPos.col++;
        }
    }
    return vecResult;
}

void File::_ToMatch(int nPos,string& data,Pos dataPos,vecPos& vecResult)
{
    string src = "";
    //先获取和pData同样大小的string
    if (_GetMatchDataFromFile(nPos,src,data.size()))
    {
        //匹配
        if(_Match(src,data))
        {
            vecResult.push_back(dataPos);
        }
    }
    fseek(m_fp,nPos+1,SEEK_SET);
}

bool File::_Match(string& src,string& res)
{
    for (int i=0;i<res.size();i++)
    {
        if (src[i]!=res[i])
        {
            return false;
        }
    }
    return true;
}

bool File::_GetMatchDataFromFile(int nPos,string& src,int nLength)
{
    if(_OpenFile("r+"))
    {
        src = "";
        char temp[2];
        fseek(m_fp,nPos,SEEK_SET);
        char ch;
        for (int i=0;i<nLength;i++)
        {
            if(!feof(m_fp))
            {
                ch=fgetc(m_fp);
                temp[0] = ch;
                temp[1] = '\0';
                src.append(temp);
            }
            else
                return false;
        }
        return true;
    }
    return false;
}

相关文章

  • Android 基础之文件和数据库

    Java 文件流 1. 字节流文件操作(读写)的代码 2. 字符流文件操作(读写)的代码 3. 按行(读写)的代码...

  • 文件读写--代码

    在写代码之前,我大概想了一下,文件操作大多是文件的读写操作,所以就先把最基本的读写操作的部分写了一下,包括: Fi...

  • flink之dataset的文件数据源

    读写文件的操作,简单 一,代码 二,输入文件 三,输出文件

  • Lua读写文件范例

    文件读写 文件读写对制作游戏很有帮助。可以调用别的文件中的代码,保存最高分、游戏存档、玩家状态等信写到文件中。 首...

  • 文件读写-python学习23

    文件读写 文件读写:是Python代码调用电脑文件的主要功能,能被用于读取和写入文本记录、音频片段、Excel文档...

  • OC语言day08-06NSArray文件读写

    pragma mark NSArray文件读写 pragma mark 概念 pragma mark 代码

  • 实现一个简单的文件系统(文件读写)

    本文将介绍基于libfuse怎么实现一个文件的读写,以及文件读写的流程是怎样的。 1.文件写入 在代码中对一个文件...

  • 笨办法学python16-26

    exercise 16 读写文件 看起来代码就很长的样子作业代码: 运行结果: 同时,打开文件,确实实现了txt文...

  • Python对txt、csv以及xml文件读写数据

    Python对常用txt、csv以及xml文件读写数据,直接上代码

  • PythonのIO操作

    IO操作,即对硬盘上的数据进行读写操作。直接上代码: 对于读写操作,最后一定要关闭文件对象,因为文件对象会占用操作...

网友评论

      本文标题:文件读写--代码

      本文链接:https://www.haomeiwen.com/subject/hhnhoxtx.html