其他分享
首页 > 其他分享> > 读写文件

读写文件

作者:互联网

#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]=='\0')
    {
        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

标签:文件,src,int,读写,readfile,char,iter,include
来源: https://blog.51cto.com/u_14582976/2829183