何处流连 2017-03-03 00:48 采纳率: 0%
浏览 3634
已结题

c++结构体数组赋值问题

使用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 搜索网站
.
.
这种输出。

  • 写回答

3条回答

  • 伪造的时空 2017-03-03 01:45
    关注

    GetLineAndPrint函数中 url[i].name=str.data(); 这一句有问题,str只是局部变量,函数退出后就会释放,数据就没了,这里应申请空间

    void GetLineAndPrint(string in_name)  
    {   
        //string tname;
        ifstream fin(in_name.c_str());
        int l = in_name.size();
        string tname(in_name.substr(0,l-4));
        int pos1 = tname.find_last_of('\\');
        string name(tname.substr(pos1+1));
        ofstream fout("1.txt",ios::app);
        if(!fin)  
        {  
            cerr<<"open file error"<<endl;  
            exit(-1);  
        }  
        string str;  
        int i=0;
        while(getline(fin,str))  
        {  
            fout<<name<<' '<<str<<endl;
            char * ss = new char[str.length()];//申请空间
            strcpy(ss,(char *)str.data());//赋值
            url[i].name=ss;
            i++;
        }  
        fin.close();
        fout.close();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?