在wc program c ++中中止(core dumped)

时间:2017-04-11 00:14:52

标签: c++

我试图在c ++中实现wc命令,但每当我尝试运行程序时,我都会出现中止(核心转储)错误。它编译没有错误,所以我不确定错误在哪里。任何代码样式的改进也将受到赞赏。

#include <iostream>
#include <string>
#include <cstdlib>
#include <string>
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>

using namespace std;
int main(int argc, char *argv[])
{                                                                                               
    setvbuf(stdout, NULL, _IONBF, 0);
    int c_flag=0, l_flag=0, m_flag=0, w_flag=0;
    int bytes;
    int lines;
    int characters;
    int words;
    int ch;
    FILE *fp;

    opterr = 0;
    while((ch = getopt(argc, argv, "clmw::")) != 1)
    {
        switch (ch)
        {
            case 'c':
                c_flag=1;
                break;
            case 'l':
                l_flag=1;
                break;
            case 'm':
                m_flag=1;
                break;
            case 'w':
                w_flag=1;
                break;
            case '?':
                fprintf(stderr, "wc: invalid option -- '%c'\nTry `wc --help' for more information.", optopt);
                return EXIT_FAILURE;
            default:
                abort();
        }
    }
    if ((c_flag == 1) && (m_flag == 1))
    {
        fprintf(stderr, "wc: cannot have both -c and -m flags\nTry `wc --help' for more information.");
            return EXIT_FAILURE;
    }
    int i = optind;
    if (i == optind)
    {
        printf("read stdin");
    }
    else
    {
        for (int i = optind; i < argc; ++i)
        {
            string input;
            input = string(argv[i]);
            if (input == "-") 
            {
                string buf;
                char ch;
                while(cin.get(ch))
                {
                    buf += ch;
                } 
                printf("read stdin");  
                if (c_flag) printf("c flag");
                if (l_flag) printf("l flag");
                if (m_flag) printf("m flag");
                if (w_flag) printf("w flag");
            }
            else
            {
                fp = fopen(argv[i], "r");
                if (fp)
                {
                    fseek(fp, 0L, SEEK_END);
                    bytes = ftell(fp);
                    rewind(fp); 
                    while(!feof(fp))
                    {
                        ch = fgetc(fp);
                        if (isspace(ch)) ++words;
                        while(isspace(ch))
                        {
                            if(ch == '\n') ++lines;
                        }
                        ++characters;
                    }
                    printf("%d", lines);
                    printf("%d", words);
                    printf("%d", characters);
                    printf("%d", bytes);
                    if (c_flag) printf("c flag");
                    if (l_flag) printf("l flag");
                    if (m_flag) printf("m flag");
                    if (w_flag) printf("w flag");
                }
                else
                {
                    fprintf(stderr, "wc: %s: No such file or directory ", argv[i]); return EXIT_FAILURE;
                }                    
            }
        }
    }
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:0)

检查getopt

的返回值时出错
while((ch = getopt(argc, argv, "clmw::")) != 1)

Per the function's specification,当没有更多的命令行参数要处理时,它返回-1。由于此条件无效,您的switch会落入default并致电abort