By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

running a system function from c++ code

P: n/a

hi
how can I use $wc -l file_name which puts out the number of lines.

#include <cstdlib>
using std::system;

system (wc -l file_name)

I get
************************************************** **************
read_data.cpp: In member function 'int read_data::get_number_of_lines()':
read_data.cpp:14: error: 'wc' was not declared in this scope
read_data.cpp:14: error: 'l' was not declared in this scope
************************************************** **************

thanks
Jul 20 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
* Gary Wessle:
>
how can I use $wc -l file_name which puts out the number of lines.

#include <cstdlib>
using std::system;

system (wc -l file_name)

I get
************************************************** **************
read_data.cpp: In member function 'int read_data::get_number_of_lines()':
read_data.cpp:14: error: 'wc' was not declared in this scope
read_data.cpp:14: error: 'l' was not declared in this scope
************************************************** **************
You need a 'main' function, and you need to supply the argument to
'system' as a string, that is, in quotes, and a semicolon after the command.

In short, you need a beginner's book.

Do you have one?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 20 '06 #2

P: n/a
"Alf P. Steinbach" <al***@start.nowrites:
* Gary Wessle:
how can I use $wc -l file_name which puts out the number of lines.
#include <cstdlib>
using std::system;
system (wc -l file_name)
I get
************************************************** **************
read_data.cpp: In member function 'int read_data::get_number_of_lines()':
read_data.cpp:14: error: 'wc' was not declared in this scope
read_data.cpp:14: error: 'l' was not declared in this scope
************************************************** **************

You need a 'main' function, and you need to supply the argument to
'system' as a string, that is, in quotes, and a semicolon after the
command.

In short, you need a beginner's book.

Do you have one?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
thanks
it would be good if I can give this complete example
first the error and the code.

ps: I have Thinking IN C++ second ed. vol1 and vol2 by Bruce Eckel,
as well as "The C++ Programming Language by Stroustrup"
I am going through the first now and doing my own self assigned
thing.

ok, here are the code

**************** the error ****************
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ read_data.o -o proj
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crt1.o: In function `_start':
.../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [proj] Error 1
************************************************** **************

**************** the code in 4 files ****************

****************read_data.h ****************
#ifndef READ_DATA_H
#define READ_DATA_H
#include <string>
using std::string;
class read_data
{
int number_of_lines;
string file_name;

public:
read_data(string);
~read_data();
int get_number_of_lines();

};
#endif

**************** read_data.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include "read_data.h"

#include <cstdlib>
using std::system;

read_data::read_data(string s) {
file_name = s;
}

int read_data::get_number_of_lines() {
number_of_lines = system ("wc -l file_name");
return number_of_lines;
}
**************** read_data_test.cpp ****************
#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../data/ZB/Jun06/20060405"; // or get it from the
// command line
read_data file1(file_name);
file1.get_number_of_lines;
}

**************** the makefile ****************
OBJS = read_data.o
COMP = g++

#### linker section ####
proj: $(OBJS)
$(COMP) $(OBJS) -o proj
########################
#### compiler section ####
..SUFFIXES:.o .cpp .h
..h.o:
$(COMP) -c $<
##########################
Jul 20 '06 #3

P: n/a
Gary Wessle wrote:
"Alf P. Steinbach" <al***@start.nowrites:

>>* Gary Wessle:
>>>how can I use $wc -l file_name which puts out the number of lines.
#include <cstdlib>
using std::system;
system (wc -l file_name)
I get
*********************************************** *****************
read_data.cpp: In member function 'int read_data::get_number_of_lines()':
read_data.cpp:14: error: 'wc' was not declared in this scope
read_data.cpp:14: error: 'l' was not declared in this scope
*********************************************** *****************

You need a 'main' function, and you need to supply the argument to
'system' as a string, that is, in quotes, and a semicolon after the
command.

In short, you need a beginner's book.

Do you have one?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


thanks
it would be good if I can give this complete example
first the error and the code.

ps: I have Thinking IN C++ second ed. vol1 and vol2 by Bruce Eckel,
as well as "The C++ Programming Language by Stroustrup"
I am going through the first now and doing my own self assigned
thing.

ok, here are the code
You still don't have a main() function. Add one.

--
Ian Collins.
Jul 20 '06 #4

P: n/a
Gary Wessle <ph****@yahoo.comwrites:
"Alf P. Steinbach" <al***@start.nowrites:
* Gary Wessle:
how can I use $wc -l file_name which puts out the number of lines.
#include <cstdlib>
using std::system;
system (wc -l file_name)
I get
************************************************** **************
read_data.cpp: In member function 'int read_data::get_number_of_lines()':
read_data.cpp:14: error: 'wc' was not declared in this scope
read_data.cpp:14: error: 'l' was not declared in this scope
************************************************** **************
You need a 'main' function, and you need to supply the argument to
'system' as a string, that is, in quotes, and a semicolon after the
command.

In short, you need a beginner's book.

Do you have one?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

thanks
it would be good if I can give this complete example
first the error and the code.

ps: I have Thinking IN C++ second ed. vol1 and vol2 by Bruce Eckel,
as well as "The C++ Programming Language by Stroustrup"
I am going through the first now and doing my own self assigned
thing.

ok, here are the code

**************** the error ****************
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ read_data.o -o proj
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crt1.o: In function `_start':
../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [proj] Error 1
************************************************** **************

**************** the code in 4 files ****************

****************read_data.h ****************
#ifndef READ_DATA_H
#define READ_DATA_H
#include <string>
using std::string;
class read_data
{
int number_of_lines;
string file_name;

public:
read_data(string);
~read_data();
int get_number_of_lines();

};
#endif

**************** read_data.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include "read_data.h"

#include <cstdlib>
using std::system;

read_data::read_data(string s) {
file_name = s;
}

int read_data::get_number_of_lines() {
number_of_lines = system ("wc -l file_name");
return number_of_lines;
}
**************** read_data_test.cpp ****************
#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../data/ZB/Jun06/20060405"; // or get it from the
// command line
read_data file1(file_name);
file1.get_number_of_lines;
}

**************** the makefile ****************
OBJS = read_data.o
COMP = g++

#### linker section ####
proj: $(OBJS)
$(COMP) $(OBJS) -o proj
########################
#### compiler section ####
.SUFFIXES:.o .cpp .h
.h.o:
$(COMP) -c $<
##########################
in read_data_test.cpp if fixed this line
file1.get_number_of_lines();

I added a destructor definition to read_data.cpp

in makefile I fixed this
OBJS = read_data.o read_data_test.o

now the only problem is

fred@debian:~/myPrograms/common$ make
g++ -c -o read_data_test.o read_data_test.cpp
g++ read_data.o read_data_test.o -o proj
fred@debian:~/myPrograms/common$ ./proj
wc: file_name: No such file or directory
fred@debian:~/myPrograms/common$
Jul 20 '06 #5

P: n/a
Ian Collins <ia******@hotmail.comwrites:
Gary Wessle wrote:
"Alf P. Steinbach" <al***@start.nowrites:

>* Gary Wessle:

how can I use $wc -l file_name which puts out the number of lines.
#include <cstdlib>
using std::system;
system (wc -l file_name)
I get
************************************************ ****************
read_data.cpp: In member function 'int read_data::get_number_of_lines()':
read_data.cpp:14: error: 'wc' was not declared in this scope
read_data.cpp:14: error: 'l' was not declared in this scope
************************************************ ****************

You need a 'main' function, and you need to supply the argument to
'system' as a string, that is, in quotes, and a semicolon after the
command.

In short, you need a beginner's book.

Do you have one?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

thanks
it would be good if I can give this complete example
first the error and the code.

ps: I have Thinking IN C++ second ed. vol1 and vol2 by Bruce Eckel,
as well as "The C++ Programming Language by Stroustrup"
I am going through the first now and doing my own self assigned
thing.

ok, here are the code
You still don't have a main() function. Add one.

--
Ian Collins.
isn't this considered "a main() function"?

**************** read_data_test.cpp ****************
#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../data/ZB/Jun06/20060405"; // or get it from the
// command line
read_data file1(file_name);
file1.get_number_of_lines;
}
Jul 20 '06 #6

P: n/a
Gary Wessle wrote:
Ian Collins <ia******@hotmail.comwrites:
>>
You still don't have a main() function. Add one.

--
Ian Collins.


isn't this considered "a main() function"?
Sorry, I didn't scroll down far enough! Looks like you forgot to link
the two cpp files, probably easier just to type

g++ -o test read_data.cpp read_data_test.cpp

**************** read_data_test.cpp ****************
#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../data/ZB/Jun06/20060405"; // or get it from the
// command line
read_data file1(file_name);
file1.get_number_of_lines;
}

--
Ian Collins.
Jul 20 '06 #7

P: n/a
* Gary Wessle:
"Alf P. Steinbach" <al***@start.nowrites:
>* Gary Wessle:
>>how can I use $wc -l file_name which puts out the number of lines.
#include <cstdlib>
using std::system;
system (wc -l file_name)
I get
************************************************ ****************
read_data.cpp: In member function 'int read_data::get_number_of_lines()':
read_data.cpp:14: error: 'wc' was not declared in this scope
read_data.cpp:14: error: 'l' was not declared in this scope
************************************************ ****************
You need a 'main' function, and you need to supply the argument to
'system' as a string, that is, in quotes, and a semicolon after the
command.

In short, you need a beginner's book.

Do you have one?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

thanks
it would be good if I can give this complete example
first the error and the code.

ps: I have Thinking IN C++ second ed. vol1 and vol2 by Bruce Eckel,
as well as "The C++ Programming Language by Stroustrup"
I am going through the first now and doing my own self assigned
thing.

ok, here are the code

**************** the error ****************
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ read_data.o -o proj
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crt1.o: In function `_start':
../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [proj] Error 1
************************************************** **************

**************** the code in 4 files ****************
You forgot to compile and link the 'read_data_test.cpp' file.

>
****************read_data.h ****************
#ifndef READ_DATA_H
#define READ_DATA_H
#include <string>
using std::string;
Better not have using-directives in header files.

class read_data
{
int number_of_lines;
string file_name;

public:
read_data(string);
Best to pass that argument as 'std::string const&', so you avoid a copy
operation.

~read_data();
int get_number_of_lines();
Style issue: I'd just call that 'number_of_lines'.

Design issue: presumably it doesn't change any member data in the
object, so should be declared 'const', like so:

int number_of_lines() const;

Then it can be called on a 'const' object.

};
#endif

**************** read_data.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include "read_data.h"

#include <cstdlib>
using std::system;
OK in implementation file.

read_data::read_data(string s) {
file_name = s;
}
Better use an initializer list, like so:

read_data::read_data( string const& s )
: file_name( s )
{}

Doesn't buy you much in this particular situation (just one avoided
construction), but in general it can make the difference between code
that compiles, and code that doesn't.

int read_data::get_number_of_lines() {
number_of_lines = system ("wc -l file_name");
return number_of_lines;
The 'system' function doesn't work like that. Or rather, the 'wc'
command doesn't work like that. 'system' return the exit code of the
command executed, so you need to turn the 'wc' textual output into an
exit code for this to have a /chance/ to work; however, be aware that
the process exit code is usually limited to the range of one byte (C++
doesn't define this, we're in in-practice-land), which is not much.

Another way to use 'wc' is to direct its output to a file, and then read
that result file from your program.
}
**************** read_data_test.cpp ****************
#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../data/ZB/Jun06/20060405"; // or get it from the
// command line
read_data file1(file_name);
file1.get_number_of_lines;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 20 '06 #8

P: n/a
Gary Wessle <ph****@yahoo.comwrites:
Ian Collins <ia******@hotmail.comwrites:
Gary Wessle wrote:
"Alf P. Steinbach" <al***@start.nowrites:
>
>
>>* Gary Wessle:
>>
>>>how can I use $wc -l file_name which puts out the number of lines.
>>>#include <cstdlib>
>>>using std::system;
>>>system (wc -l file_name)
>>>I get
>>>*********************************************** *****************
>>>read_data.cpp: In member function 'int read_data::get_number_of_lines()':
>>>read_data.cpp:14: error: 'wc' was not declared in this scope
>>>read_data.cpp:14: error: 'l' was not declared in this scope
>>>*********************************************** *****************
>>
>>You need a 'main' function, and you need to supply the argument to
>>'system' as a string, that is, in quotes, and a semicolon after the
>>command.
>>
>>In short, you need a beginner's book.
>>
>>Do you have one?
>>
>>--
>>A: Because it messes up the order in which people normally read text.
>>Q: Why is it such a bad thing?
>>A: Top-posting.
>>Q: What is the most annoying thing on usenet and in e-mail?
>
>
thanks
it would be good if I can give this complete example
first the error and the code.
>
ps: I have Thinking IN C++ second ed. vol1 and vol2 by Bruce Eckel,
as well as "The C++ Programming Language by Stroustrup"
I am going through the first now and doing my own self assigned
thing.
>
ok, here are the code
>
You still don't have a main() function. Add one.

--
Ian Collins.

isn't this considered "a main() function"?

**************** read_data_test.cpp ****************
#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../data/ZB/Jun06/20060405"; // or get it from the
// command line
read_data file1(file_name);
file1.get_number_of_lines;
}
the only problem "I hope" that I am having now is how to pass a
variable to system as in system ("wc -l file_name") here file_name is
taken literal by the compiler where as I meant to use it as a string
variable which has the name of the file.
Jul 20 '06 #9

P: n/a
Gary Wessle <ph****@yahoo.comwrites:
it would be good if I can give this complete example
first the error and the code.
You need to check the manual for the system() call.

system() returns -1 on error, and the return status of the command
otherwise (according the Linux manual page).
Now wc probably returns 0 on success and 1 on failure. Note, this is
what it returns (if you are in UNIX, type echo $? to see what it
returned), not what it outputted.
ok, here are the code

**************** the error ****************
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ read_data.o -o proj
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crt1.o: In function `_start':
../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [proj] Error 1
************************************************** **************
You never linked in read_data_test.cpp where main() is implemented.
int read_data::get_number_of_lines() {
number_of_lines = system ("wc -l file_name");
return number_of_lines;
}
This will return whether wc succeeded or not, not the number of lines.

--
John L. Fjellstad
web: http://www.fjellstad.org/ Quis custodiet ipsos custodes
Replace YEAR with current four digit year
Jul 20 '06 #10

P: n/a
Gary Wessle wrote:
Gary Wessle <ph****@yahoo.comwrites:
>Ian Collins <ia******@hotmail.comwrites:
Gary Wessle wrote:
"Alf P. Steinbach" <al***@start.nowrites:
* Gary Wessle:

how can I use $wc -l file_name which puts out the number of lines.
#include <cstdlib>
using std::system;
system (wc -l file_name)
I get
*********************************************** *****************
read_data.cpp: In member function 'int
read_data::get_number_of_lines()': read_data.cpp:14: error: 'wc' was
not declared in this scope read_data.cpp:14: error: 'l' was not
declared in this scope
*********************************************** *****************

You need a 'main' function, and you need to supply the argument to
'system' as a string, that is, in quotes, and a semicolon after the
command.

In short, you need a beginner's book.

Do you have one?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
thanks
it would be good if I can give this complete example
first the error and the code.

ps: I have Thinking IN C++ second ed. vol1 and vol2 by Bruce Eckel,
as well as "The C++ Programming Language by Stroustrup"
I am going through the first now and doing my own self assigned
thing.

ok, here are the code

You still don't have a main() function. Add one.

--
Ian Collins.

isn't this considered "a main() function"?

**************** read_data_test.cpp ****************
#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../data/ZB/Jun06/20060405"; // or get it from the
// command line
read_data file1(file_name);
file1.get_number_of_lines;
}

the only problem "I hope" that I am having now is how to pass a
variable to system as in system ("wc -l file_name") here file_name is
taken literal by the compiler where as I meant to use it as a string
variable which has the name of the file.
What about:

std::string file_name = "whatever_file_you_want";
std::string command = "wc -l";
std::system( ( command + " " + file_name ).c_str() );
Best

Kai-Uwe Bux

Jul 20 '06 #11

P: n/a
thanks alot
Jul 20 '06 #12

P: n/a

I did as much I understand, able to compile but does not give any
output.

**************** read_data.h ****************
#ifndef READ_DATA_H
#define READ_DATA_H
#include <string>

class read_data
{
int number;
std::string file_name;

public:
read_data(std::string const&);
~read_data();
int number_of_lines();

};

#endif

**************** read_data.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include "read_data.h"

using namespace std;

read_data::read_data( string const& s )
: file_name( s )
{}

read_data::~read_data() {}

int read_data::number_of_lines() {
ifstream in(file_name.c_str());
string line;
number = 0;
while (getline(in, line))
number++;
return number;

}

**************** read_data_test.cpp ****************

#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../../data/ZB/Jun06/20060405";

read_data file1(file_name);
file1.number_of_lines();
}

**************** makefile ****************
OBJS = read_data.o read_data_test.o
COMP = g++

#### linker section ####
proj: $(OBJS)
$(COMP) $(OBJS) -o proj
########################
#### compiler section ####
..SUFFIXES:.o .cpp .h
..h.o:
$(COMP) -c $<
##########################
Jul 20 '06 #13

P: n/a
* Gary Wessle:
I did as much I understand, able to compile but does not give any
output.
That's perhaps because you don't have any output statement?

Here's a simple C++ program with output:

#include <iostream>
#include <ostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
}
>
**************** read_data.h ****************
#ifndef READ_DATA_H
#define READ_DATA_H
#include <string>

class read_data
{
int number;
std::string file_name;

public:
read_data(std::string const&);
~read_data();
int number_of_lines();

};

#endif
This is OK. Possible improvement: making 'number_of_lines' a 'const'
function.

**************** read_data.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include "read_data.h"

using namespace std;

read_data::read_data( string const& s )
: file_name( s )
{}

read_data::~read_data() {}

int read_data::number_of_lines() {
ifstream in(file_name.c_str());
string line;
number = 0;
while (getline(in, line))
number++;
return number;
}
This is also OK, although might be unable to tackle Really Long Lines
(like, a one billion character line).

**************** read_data_test.cpp ****************

#include <string>
#include "read_data.h"

using namespace std;

int main() {
string file_name = "../../data/ZB/Jun06/20060405";

read_data file1(file_name);
file1.number_of_lines();
This does nothing more than call 'number_of_lines'. You need to output
the result. See example above.

When you get that to work it's time to add some error handling, but
don't worry about that.

First get the program to work.
}

**************** makefile ****************
OBJS = read_data.o read_data_test.o
COMP = g++
It's a good idea to add options to make the compiler more stringent than
by default. And then ensure that your code compiles without warnings.
For g++ try adding '-ansi -pedantic -O -Wall' to get more warnings that
tell you about potential problems, then ensure none of them appear.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 20 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.