一个比较好的文件操作的例子

来源:互联网 发布:asp手机版网站源码 编辑:程序博客网 时间:2024/06/02 16:08

#include "stdafx.h"
#include<stdio.h>
#include<string>
#include<iostream>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <cctype>
using namespace std;

//定义存目标字符信息(行,列,相邻字符)
class Pos
{
public:
    double x, y;
    string str;
    Pos()
    {
      x = 0;
      y = 0;
    }
};

string stringToLower(string s)
{
    string::iterator s_iter = s.begin();
    while ( s_iter != s.end() )
    {
      *s_iter = tolower(*s_iter);
      ++s_iter;
    }
      
    return s;
}

char* stringToLower(char ch[])
{
    for (char *p_ch = ch; *p_ch != '/0'; ++p_ch)
    {
      *p_ch = tolower(*p_ch);
    }

    return ch;
}

//把带查找的字符串存储到set容器当中
//参数:(带查找字符串,容器)
void separateWordsIntoSet(const string &str, set<string> &s)
{
    istringstream ssin(str);
    string word;
    while (ssin >> word)
    {
      s.insert( stringToLower(word) );
    }
}

//解析传入参数,把参数存放在set当中
//参数:(argc, argv, 待查找的文件名, set容器)
void separateAgrv(int argc, char *argv[], string &filename, set<string> &s)
{
    filename = argv[1];
    for (int i = 2; i != argc; ++i)
    {
      s.insert( stringToLower(argv[i]) );
    }
}


//搜索目标字符串,并存储结果于multimap中
//参数:(待搜索文件名,目标字符串,存储容器)
void searchWordsInTheFile(const string filename, const set<string> &target, multimap<string, Pos> &m)
{
    ifstream fin(filename.c_str() );
    istringstream ssin;
    string str_line, word;
    double x = 0, y = 0;//当前行,列
    double x_last = 0, y_last = 0;//前一个单词的行,列
    bool found = 0;//记录上一个单词是否属于待查找单词
    string str_front, str_mid;//记录待查找单词的前后信息

    if (!fin)
    {
      cerr << "Can not open file : " << filename << endl;
      exit(0);
    }


    //分行读入,每次处理一行,节省内存空间
    while (fin.good() && !fin.eof() )
    {
      ++x;//记录行
      y = 0;//记录列
      getline(fin, str_line);//读入一行
      cout << x << "/t" << str_line << endl;
      ssin.str(str_line);
      Pos p;  

      while (ssin >> word)//处理一行单词
      {
            ++y;//记录列
            if (found )
            {
                   p.x = x_last;//存储找到的单词的行列
                   p.y = y_last;
                   p.str = str_front + " " + str_mid + " " + word;//单词前后信息
                   m.insert(make_pair(str_mid, p) );
            }

            if ( target.find( stringToLower(word) )   != target.end() )
            {
                   found = true;
            }
            else
            {
                   found = false;
            }

            str_front = str_mid;//记录上个单词信息
            str_mid = word;
            x_last = x;
            y_last = y;
      }
      ssin.clear();
    }

    fin.close();
}

int main(int argc, char *argv[])
{
    set<string> s;
    multimap<string, Pos> m;//存储找到的单词信息
    string str_temp, filename;    

    switch (argc)
    {
      case 1 ://没有传入参数
            cout << "Please input the filename :";
            cin >> filename;
            cin.ignore();
            cin.clear();

            cout << "Please input the words your want to find :";
            getline(cin, str_temp);
            separateWordsIntoSet(str_temp, s);          

            break;
      
      case 2 ://如果传入了文件名和单词
            cout << "Please input the words your want to find :";
            getline(cin, str_temp);
            filename = argv[1];
            separateWordsIntoSet(str_temp, s);       

            break;

      default ://如果传入了文件名和单词
            separateAgrv(argc, argv, filename, s);
    }    

    cout << "===========================================" << endl;

    searchWordsInTheFile(filename, s, m);//从filename搜索s内的单词,把结果存放到m

    //输出结果

    cout << "===========================================" << endl;

    multimap<string, Pos>::iterator m_iter = m.begin();
    while (m_iter != m.end() )
    {
       cout << (m_iter->second).x << " , " <<
(m_iter->second).y <<"/t/t/t/t" <<
(m_iter->second).str << endl;
      ++m_iter;
    }

    return EXIT_SUCCESS;

}