" COUT"应该显示队列的前后,而不是显示" val"的值。在推送功能和" line1"在scanitall功能。为什么?

时间:2016-04-27 14:59:19

标签: c++ pointers memory-management queue file-handling

我一直在尝试调试此代码超过两周..
此代码是我的项目的一部分&#34;离线系统爬虫&#34;。目前,关注的功能是:< BR />

  1. &#34; scanitall&#34; function打开文件&#34; outputproject.txt&#34;并将所有元素逐行插入由全局变量&#34; * front&#34;定义的队列中。和&#34; *后&#34;这是节点类型。
  2. 创建队列后,功能&#34; processit&#34;进一步获取文件&#34; inputproject.txt&#34; line作为参数,然后标识文件中的链接(链接在文本文件中的类型为** link **),并进一步将其附加到&#34; outputproject.txt&#34;。
    现在的问题是,推送中的cout总是显示&#34; val&#34;
    的值(而不是&#34;后面&gt;值&#34;和&#34; front-&gt; value&#34;)
    作为参数传递给函数,类似地&#34; scanitall&#34; function cout显示&#34; line1&#34;
    的值(而不是&#34; rear-&gt; value&#34;&#34; front-&gt; value&#34 ;)

    #include<iostream>
    #include<fstream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    ifstream infile;
    ofstream outfile;
    struct node
    {
        struct node *next;
        char *value;
    }*front,*rear;
    /*struct node * createnode(char *val)
    {
        struct node *new1=new node;
        new1->next=NULL;
        new1->value=val;
        return new1;
    }*/
    void push(char *val)
    {
        cout<<"\nPush Called with argument : "<<val<<endl;
        if(front && rear)
            cout<<"\nPush called->rear->"<<rear->value<<"->front->"<<front->value<<" val-> "<<val<<endl;
        struct node *new1=new node;
        new1->next=NULL;
        new1->value=val;
        if(front==NULL && rear==NULL)
        {
            rear=new node;
            front=new node;
            rear=new1;
            front=new1;
            cout<<"\nCreated a node 1\n";
        }
        else
        {
            rear->next=new1;
            rear=rear->next;
            //front->next=NULL;
            cout<<"\ncreated a node 2\n";
        }
        cout<<"Rear->"<<rear->value<<" =>> Front->"<<front->value<<"\n";
    }
    /*char *pop()
    {
        cout<<"Pop called\n";
        char *ch;
        if(front==NULL)
            return NULL;
        else if(front==rear)
        {
            ch=front->value;
            front=NULL;
            rear=NULL;
            return ch;
        }
        else
        {
            ch=front->value;
            front=front->next;
            return ch;
        }
    }*/
    void scanitall()
    {
        cout<<"scanitall called\n";
        string line;
        infile.open("outputproject.txt");
        while(getline(infile,line))
        {
            char line1[100];
            strncpy(line1, line.c_str(), sizeof(line1));
            line1[sizeof(line1) - 1] = 0;
            if(rear)
                cout<<"\n--------Rear->"<<rear->value<<"--------front->"<<front->value<<"-----\n";
            else
                cout<<"\n----------Rear empty at this time---------\n";
            push(line1);
        }
        infile.close();
    }
    void processit(char *abc)
    {
        // cout<<"PRocess it called\n";
        outfile.open("outputproject.txt",ios::app);
        if(!outfile)
            cout<<"Error file not opened!";
        int i,len,flag=0;
        len=strlen(abc);
        for(i=0;i<len;i++)
        {
            if(abc[i]=='*' && abc[i+1]=='*' && flag==0)
            {
                flag=1;
                i+=2;
            }
    
            if(abc[i]=='*' && abc[i+1]=='*' && flag==1)
            {
                outfile<<endl;
                flag=0;
                i+=2;
            }
    
            if(flag==1)
                outfile<<abc[i];
        }
        outfile.close();
    }
    void display(char *line)
    {
        // cout<<"display called\n";
        string line2;
        char ch;
        infile.open(line);
        if(!infile)
            cout<<"File not opened!  ";
        while(getline(infile,line2))
        {
            cout<<line2;
            cout<<endl;
        }
        cout<<endl;
        cout<<endl;
        infile.close();
    }
    void openallfiles()
    {
        // cout<<"openallfiles called\n";
        string line;
        char line1[100];
        infile.open("outputproject.txt");
        while(getline(infile,line))
        {
            strncpy(line1, line.c_str(), sizeof(line1));
            line1[sizeof(line1) - 1] = 0;
            display(line1);
        }
        infile.close();
    }
    int main()
    {
        front=NULL;
        rear=NULL;
        scanitall();
        string line;
        char line1[100];
        infile.open("inputproject.txt");
        while(getline(infile,line))
        {
            strncpy(line1, line.c_str(), sizeof(line1));
            line1[sizeof(line1) - 1] = 0;
            processit(line1);
        }
        infile.close();
        openallfiles();
        return 0;
    }
    

0 个答案:

没有答案