使用getline把处理的数据输出到txt文档中,变成如下格式:
搜索 www.baidu.com
搜索 www.goole.com
电商 www.baidu.com
.
.
现在要用结构体数组存储,
typedef struct tagMember
{
char *name;
char *group;
};
struct tagMember url[20];
然后我读取生成的txt文件后出现内存报错,也不懂赋值,求大神解惑。
代码如下,我是学java的,C++的指针和结构体迷迷糊糊的
#include <atlstr.h>
#include <winbase.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <vector>
#include <cstring>
#include <io.h>
#include <set>
using namespace std;
typedef struct tagMember
{
char *name;
char *group;
};
tagMember url[500];
void GetLineAndPrint(string in_name)
{
//string tname;
ifstream fin(in_name);
int l = in_name.size();
string tname(in_name.substr(0,l-4));
//int pos = in_name.find('.');
//if(pos>0)
//tname = in_name.left(pos);
int pos1 = tname.find_last_of('\\');
string fname(tname.substr(pos1+1));
//ofstream fout("1.txt",ios::app);
//typedef set<string> set_t;
//set_t s;
if(!fin)
{
cerr<<"open file error"<<endl;
exit(-1);
}
string str;
int i=0;
while(getline(fin,str))
{
char * ss = new char[str.length()];//申请空间
char * tt = new char[str.length()];
strcpy(ss,(char *)str.data());//赋值
strcpy(tt,(char *)fname.data());
url[i].name=ss;
url[i].group=tt;
i++;
//s.insert(str);
//t.insert(name);
//cout<<name<<' '<<str<<endl;
}
//for(set_t::const_iterator p = s.cbegin();p!=s.cend();++p)
//{
// //set_t t;
// cout<<*p<<' '<<name<<endl;
//}
}
typedef list<char*> ListType;
typedef struct ListNode
{
char* groupName;
ListType *next;
}NodeType;
typedef list<NodeType*> myList;
typedef myList::iterator myIter;
myIter myFind(myList& mylist, char* dest)
{
myIter it;
for (it=mylist.begin(); it != mylist.end(); it++)
{
if( (*it)->groupName == dest)
return it;
}
return it;
}
int main()
{
struct _finddata_t fileinfo;
string in_path;
string in_name;
cin>>in_path;
string curr = in_path+"\\*.txt";
long handle;
if((handle=_findfirst(curr.c_str(),&fileinfo))==-1L)
{
cout<<"没有找到匹配文件!"<<endl;
system("pause");
return 0;
}
else
{
in_name = in_path + "\\" + fileinfo.name ;
GetLineAndPrint(in_name);
while(!(_findnext(handle,&fileinfo)))
{
in_name = in_path + "\\" +fileinfo.name;
GetLineAndPrint(in_name);
}
_findclose(handle);
}
myList classInfo;
NodeType* pNode;
//插入节点
for(size_t i=0; i<5; i++)
{
//创建一个索引节点
myIter it = myFind(classInfo, url[i].group);
if( it!= classInfo.end() ) //已存在
{
//存储name
(*it)->next->push_back( url[i].name );
}
else //创建新的NodeType节点
{
pNode = new NodeType;
pNode->groupName = url[i].group;
pNode->next = new ListType; //创建list
//存储当前的name
pNode->next->push_back( url[i].name );
classInfo.push_back( pNode );
}
}
//输出:
for (myIter it=classInfo.begin(); it != classInfo.end(); it++)
{
cout << (*it)->groupName << ": ";
for ( ListType::iterator bg=(*it)->next->begin();
bg != (*it)->next->end(); bg++ )
{
cout << (*bg) << "\t";
}
cout << endl;
}
system("pause");
return 0;
}
可能我刚才说的不清楚,我是读取目录下的所有txt文件,每个.txt文件本身文件名是“搜索网站”或是“购物网站”等等。内容是
www.baidu.com
www.goole.com
www.baidu.com
.
.
这样的,
比如baidu可能出现在搜索网站.txt中,也可能出现在卖假药.txt中,而我最终目标输出是变成
www.baidu.com 搜索网站 卖假药网站
www.goole.com 搜索网站
.
.
这种输出。