Program run perfectly without error but cant show anything

This is my program,what I want is do this program to check how many bytes,lines and words inside any of the file.
When I type wc test.cpp it will pop out
6 111 12608 test
But when I type wc test.cpp | ./test -l
it give me segmentation fault(core dumped)
Anyone can help me with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main (int argc, char *argv[])
{
    string str;
    char *str1 = new char[80];
    int SUM=0,count=0;
    string fsize;
    int min=0,max=0;

    if(argc==1){
            cout<<"Not enough arguements!";

    }
    getline (cin, str);
    strcpy (str1, str.c_str());
    fsize = strtok (str1," ");
        for(int i=0;i<argc; i++){

                if(strcmp(argv[i],"-b")==1){

                    for (int m=0; m<=2; m++)
                        fsize = strtok (NULL, " ");
                    if (atoi(fsize.c_str()) > max) {
                        max = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    if (atoi(fsize.c_str()) < max) {
                        min = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    count++;}
                else if (strcmp(argv[i],"-l")==1){

                    for (int m=0; m<=2; m++)
                        fsize = strtok (NULL, " ");
                    if (atoi(fsize.c_str()) > max) {
                        max = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    if (atoi(fsize.c_str()) < max) {
                        min = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    count++;}
                else if (strcmp(argv[i],"-w")==1){

                    for (int m=0; m<=2; m++)
                        fsize = strtok (NULL, " ");
                    if (atoi(fsize.c_str()) > max) {
                        max = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    if (atoi(fsize.c_str()) < max) {
                        min = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    count++;}
                else
                    cout<<"\nInvalid Input!\n";

                cout << "There are " << count << " files" << endl;
                cout << "Largest file has " << max << " bytes" << endl;
                cout << "Smallest file has " << min << " bytes" << endl;
                cout << "The ToTal is " << SUM  << endl;
	}
	return 0;

}
Last edited on
This is my program,what I want is do this program to check how many bytes,lines and words inside any of the file.
When I type wc test.cpp it will pop out
6 111 12608 test
But when I type wc test.cpp | ./test -l
it give me segmentation fault(core dumped)
Anyone can help me with this?

I'll give you a hint.

You're running your program twice.

The first time, you run: wc test.cpp where argc = 2, argv = ["wc", "test.cpp"]

The second time, you run wc where argc = 2, argv = ["wc", "-l"] and the input comes in on stdin.
Last edited on
after I type wc test.cpp it will pop out the 3 num and 1 file name,example
12 12 50 test.cpp
after I type wc test.cpp | ./test -l
It not suppose to capture the 3 num and file name?why it will capture ["wc", "-l"] ?
argv suppose to capture the output from the command before I make command right?
Topic archived. No new replies allowed.