IGNORE last line of WC from pipe

Hi

I have these 2 files for example file1 file2

Now wc *txt gives

1 5 7 file.txt
1 2 3 file2.txt
2 7 10 total

Now I need to find the highest lines words and bytes. But always the last line is shown as the highest words, lines bytes..How can I Ignore the last line

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
int main(int argc, char* argv[]){


    int j=0,k=0;
    string file;
    char *file1=new char[80];
    char *fsize=NULL, fileWordMax[20], fileLineMax[20], fileByteMax[20];
    int maxByte=0, maxWord=0, maxLine=0, count=0;
     while (getline (cin, file)!=NULL){
            strcpy (file1, file.c_str());	//convert str to C style string (char array)

            fsize=strtok(file1, " ");

                if(atoi(fsize)>maxLine){
                    maxLine=atoi(fsize);
                    for(int j=0;j<file.length();j++){

                        if(isalpha(file[j])){
                            //cout<<file[j]<<endl;
                            fileLineMax[k]=file[j];
                            k++;
                            }

                    }
            for(int i=1;i<2;i++){
                fsize=strtok(NULL, " ");
                if(atoi(fsize)>maxWord){
                    maxWord=atoi(fsize);
                    //strcpy(fileWordMax, file1);
                    }
                }
              for(int i=2;i<4;i++){
                fsize=strtok(NULL, " ");
                if(atoi(fsize)>maxByte){
                    maxByte=atoi(fsize);
                    //strcpy(fileWordMax, file1);
                    }
                }

            }
        }


    if(argc==1){
        cout<<"insufficient arguments entered"<<endl;
        return 0;}



    for(int i=1;i<argc;i++){


        if((strcmp(argv[i], "-l")==0)){

            cout<<"Highest number of lines: "<<maxLine<<" in file "<</*fileLineMax*/<<endl;

            }
Last edited on
The word on the last line is "total".
just after first get line i used

if(file.find("total")!=string::npos)
break;


but it is not giving the desired output
This becomes a whole lot simpler if you actually commit to C++ instead of making a mishmash of C++ and C code:

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
#include <iostream>
#include <string>

struct wc_descriptor
{
    unsigned lines = 0;
    unsigned words = 0;
    unsigned chars = 0;
    std::string name;
};

// operator overload to simplify reading
std::istream& operator>>(std::istream& is, wc_descriptor& wc)
{
    is >> wc.lines >> wc.words >> wc.chars;
    return getline((is >>std::ws), wc.name);
}

// operator overload to simplify comparisons
bool operator>(const wc_descriptor& a, const wc_descriptor& b)
{
    return a.lines > b.lines;
}

int main()
{
    wc_descriptor current_wc;
    wc_descriptor max_wc;

    while (std::cin >> current_wc  &&  current_wc.name != "total")
         if (current_wc > max_wc)
             max_wc = current_wc;

    std::cout << "The highest number of lines (" << max_wc.lines;
    std::cout << ") occurred in \"" << max_wc.name << "\"\n";
}
Last edited on
your code is too advanced..wow..but thanks...!
But if i may trouble you... what should I add to also compare words and lines when I do this

for(int i=1;i<argc;i++){


if((strcmp(argv[i], "-w")==0)){

cout<<"Highest number of word: "<<maxLine<<" in file "<</*fileLineMax*/<<endl;

and bytes
i made it work..ty!
btw. I used three return statements in

bool operator>(const wc_descriptor& a, const wc_descriptor& b)
{
return a.lines > b.lines;
return a.words > b.words;
return a.chars > b.chars;
}

how come this multiple return statements happen? It works also...
Last edited on
Two of those return statements are unreachable. The only one ever executed is the first.
but it is working fine for me...I get max words, lines or bytes correctly..how
The most likely reason is that your test cases are too limited and thus fail to reveal how horribly broken your code is.
it works...thats matters for now..later I fix the error...
> The word on the last line is "total".
>> while (std::cin >> current_wc && current_wc.name != "total")
Having a file whose name is simply "total" is perfectly valid.
added if else statement in the multiple returns
ne555 wrote:
Having a file whose name is simply "total" is perfectly valid.

That is true.

However, csstudent123's standard response to almost all C++ is "your code is too advanced / our class hasn't covered that yet". That, IMHO, warrants a chance to practice a bit more.

To be "fair", lets sketch:
1. Read line A
2. WHILE read line B succeeds
  process A
  A := B
3. IF there was only one line, process A
4. print result
Keskiverto, all my response? you read 278 posts almost all responses? Do not response when you do not know about who you are responding to...
Topic archived. No new replies allowed.