Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
- The problem statement, all variables and given/known data:
No problem,but it cannot show what I want
-
Relevant commands, code, scripts, algorithms:
wc test.cpp
wc test.cpp | ./test -l -
The attempts at a solution (include all code and scripts):
#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;
getline (cin, str);
strcpy (str1, str.c_str());
fsize = strtok (str1," ");
if(argc==1){
cout<<"Not enough arguements!";
}
for(int i=0;i<argc; i++){
if(strcmp(str1,"-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,"-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,"-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;
}
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?
INTI/Interacting System