使用字符串变量的c ++分段错误

时间:2018-02-14 01:27:24

标签: c++11

我上传了整个代码,因为我无法确定分段错误的确切位置,因为分段错误是一个模糊的错误

#include<map>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <stdio.h>
    using namespace std;
    bool newl=false;
    string censormode (string line){

        int len=line.length();
    int end;
    string word;
    for(int i=0;i<len;i++){
        if(isspace(line[i])){
            word.push_back(line[i]);
            end=i;
            newl=true;
        }
        else if(isalpha(line[i])){
            word.push_back(line[i]);
            newl=true;
        }

        else{
            newl=false;
            for(int x=i-1;x>end;x--){
                word.pop_back();
            }
            while(!isspace(line[i])){
                i++;
            }
            end=i;
            while(isspace(line[i])){
                i++;
            }
            i--;
        }
    }
    return word;
}
string squishmode (string word,int j){
    if(j==0){
        return  word;
    }
    else{
        return " "+word;
    }
}

int main(int argc, char * argv[]) {

map<string,bool> flagC={{"-q",false},{"-c",false},{"-l",false},{"-s",false},{"-p",false}};
    int counter=0;
    ifstream inF;
    string lines;
    for (int i=1;i<argc;i++){
        string flag = argv[i];
        if(flag=="-q"){
            flagC["-q"]= true;
        }
        else if(flag=="-s"){
            flagC["-s"]=true;
        }
        else if(flag=="-c"){
            flagC["-c"]=true;
        }
        else if(flag=="-l"){
            flagC["-l"]=true;
        }
        else if(flag=="-p"){
            flagC["-p"]=true;
        }
        else if(flag[0]!='-'){
            counter++;
            if(counter==1){
                string file=argv[i];
                inF.open(argv[i]);

                if(!inF){
                    cout<<argv[i]<<" CANNOT OPEN"<<endl;
                    return 0;
                }
                char word;
                while(inF.get(word)){
                    lines.push_back(word);
                }

            }
        }
        else{
            cout<<flag<<" INVALID FLAG" <<endl;
            return 0;

        }
        if(counter>1){
            cout<<"TOO MANY FILES"<<endl;
            return 0;
        }
    }
    string words=lines;

    if((flagC["-q"]==true&&flagC["-c"])||(flagC["-q"]==true&&flagC["-s"]==true)){
        cout<<"CONFLICTING FLAGS"<<endl;
        return 0;
    }
    if(flagC["-q"]==true){
        return 0;
    }
    if(flagC["-s"]==true){
        string word;
        string line1;
        words.clear();
        istringstream something(lines);
        while(getline(something,line1)){
            if(line1.length()>0){
                istringstream inString(line1);
                int o=0;


                while(inString>>word){

                    words+=squishmode(word,o);

                    o++;
                }
                words+="\n";
            }
        }

    }
    if(flagC["-c"]==true){

        string cword=words;

        string cwords;
        istringstream something1(cword);
        words.clear();
        while(getline(something1,cwords)){
            words+=censormode(cwords);
            if(newl==true){
                words+="\n";

            }
        }

    }
    cout<<words;

    return 0;
}

审查如果循环导致分段错误,当命令行参数是-c文件名但是当文件通过-s循环和-c循环时它正在工作任何帮助表示赞赏

0 个答案:

没有答案