读写文件

#include <iostream>
#include <fstream>
#include <assert.h>
#include <map>
using namespace std;
int stupidKmp(const char* dest, const char* src, int pos)
{
    int i=pos;
    int j=0;
    while(dest[i+j] && src[j])
    {
        if(dest[i+j]==src[j])
        {
            j++;
        }
        else
        {
            i++;
            j=0;
        }
    }
    if(src[j]=='')
    {
        return i;
    }
    else
    {
        return -1;
    }
}

int main()
{
    map<int, int>mymap;
    char src[]="abc";
    int H=1;
    ifstream readfile("./find.cpp",ios::in);
    ofstream write;
    write.open("./newfind.cpp");
    string line;
    if(! readfile)
    {
        assert(false);//fail open
    }
    while(! readfile.eof())
    {
        getline(readfile,line);
        write<<line.c_str()<<endl;
        int V=stupidKmp(line.c_str(),src,0);
        if(V != -1)
        {
            mymap.insert(make_pair(H,V));
        }
        H++;
    }
    readfile.close();
    write.close();
    for(map<int ,int>::iterator iter=mymap.begin(); iter!=mymap.end(); iter++)
    {
        int first = iter->first;
        int second=iter->second;
        cout<< "row:"<<first<<" "<<"col:"<<second<<endl;
    }
    return 0;
}
find.cpp
aasssdafaabcdfadfaffs
dfafefaefaabcsdfefas
afefaseascabcasefwe
aerfawefasabefae
aeertawefabcasefawe
aererawfaabcwf3
aefe3faaabcwfwe
abc

关注公众号 海量干货等你
原文地址:https://www.cnblogs.com/sowhat1412/p/12734479.html