我从文件重定向输入时出现分段错误

时间:2016-09-24 00:04:36

标签: c++

这是一个计算输入频率的程序,我试图从名为1.txt的文本文件中间接输入,我的程序名是prog3_1。 但是当我在ubuntu 14.04中使用./prog3_1< 1.txt,显示分段错误。我不知道为什么。这是我的代码。

# include <iostream>
# include <string.h>
# include <iomanip>
#include <stdlib.h>

using namespace std;

int main(int argc, char** argv){
bool findc, findn, findL, findp, finds, flag;
findc = findn = findL = findp = finds = flag =false;
char *input;
int numOfOutput = 1;

for(int i = 1; i < argc; i++){
    if (strcmp(argv[i], "-c") == 0) {
        findc = true;
        continue;
    }
        if (strcmp(argv[i], "-n") == 0) {
        findn = true;
        continue;
    }
        if (strncmp(argv[i], "-L", 2) == 0) {
        findL = true;
        numOfOutput =     atoi((string(argv[i]).substr(2,strlen(argv[i])-2)).c_str());
        continue;
    }
        if (strcmp(argv[i], "-p") == 0) {
        findp = true;
        continue;
    }
    if (strcmp(argv[i], "-s") == 0) {
        finds = true;
        continue;
    }
     input = argv[i];
    }


int array[256] = {0};
for(int i = 0; i < strlen(input); i++){
    array[(int)input[i]]++;
}
int findmax = array[0];
for(int i = 0; i < 256; i++){
    if(findmax < array[i]) findmax = array[i];
}

int count = 0;
int temp = findmax;
while (temp != 0){
    temp = temp / 10;
    count++;
}
//cout << count << " "<< findmax <<endl;

if(findn){
    int outputCount = 0;
    for(int i = 0; i < 256; i++){
        if(array[i] == 0) continue;
        cout << setfill(' ') << setw(count) << array[i] << ";";
        outputCount++;
        if (outputCount % numOfOutput == 0) cout << endl;
    }
    flag = true;
}   

if(findc){
    int outputCount = 0;
    for(int i = 0; i < 256; i++){
        if(array[i] == 0) continue;
        cout << hex << setfill('0') << setw(2) << i <<"," ;
        cout << dec << setfill(' ') << setw(count) << array[i] << ";";
        outputCount++;
        if (outputCount % numOfOutput == 0) cout << endl;
    }
    flag = true;
}

if(findp){
    double percent = 0.0;
    int outputCount = 0;
    for(int i = 0; i < 256; i++){
        if(array[i] == 0) continue;
        percent = array[i] * 100.0 / (double)strlen(input);
        cout << hex << setfill('0') << setw(2) << i <<" " ;
        cout << dec <<setprecision(3) << percent << ";";
        outputCount++;
        if (outputCount % numOfOutput == 0) cout << endl;
    }
    flag = true;
}

if(finds){
    int outputCount = 0;
    int temp = 0;
    for (int i =0; i < 256; i++){
        int max = array[0];
        for (int j = 0; j < 256; j++){
            if (max < array[j]) {
                max = array[j];
                temp = j;
            }
        }
        if (max == 0) break;
        cout << hex << temp << " ";
        cout << dec << max << ";";
        array[temp] = 0;
        outputCount++;
        if (outputCount % numOfOutput == 0) cout << endl;
    }
    flag = true;
}

if(flag == false){
    int outputCount = 0;
    for(int i = 0; i < 256; i++){
        if(array[i] == 0) continue;
        cout << hex << setfill('0') << setw(2) << i <<" " ;
        cout << dec << setfill(' ') << setw(count) << array[i] << ";";
        outputCount++;
        if (outputCount % numOfOutput == 0) cout << endl;
    }
}
return 0;
}

0 个答案:

没有答案