Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > running a system function from c++ code

Reply
Thread Tools

running a system function from c++ code

 
 
Gary Wessle
Guest
Posts: n/a
 
      07-20-2006

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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-20-2006
* 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?
 
Reply With Quote
 
 
 
 
Gary Wessle
Guest
Posts: n/a
 
      07-20-2006
"Alf P. Steinbach" <(E-Mail Removed)> writes:

> * 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 $<
##########################
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-20-2006
Gary Wessle wrote:
> "Alf P. Steinbach" <(E-Mail Removed)> writes:
>
>
>>* 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.
 
Reply With Quote
 
Gary Wessle
Guest
Posts: n/a
 
      07-20-2006
Gary Wessle <(E-Mail Removed)> writes:

> "Alf P. Steinbach" <(E-Mail Removed)> writes:
>
> > * 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$
 
Reply With Quote
 
Gary Wessle
Guest
Posts: n/a
 
      07-20-2006
Ian Collins <(E-Mail Removed)> writes:

> Gary Wessle wrote:
> > "Alf P. Steinbach" <(E-Mail Removed)> writes:
> >
> >
> >>* 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;


}
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-20-2006
Gary Wessle wrote:
> Ian Collins <(E-Mail Removed)> writes:
>>
>>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.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-20-2006
* Gary Wessle:
> "Alf P. Steinbach" <(E-Mail Removed)> writes:
>
>> * 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?
 
Reply With Quote
 
Gary Wessle
Guest
Posts: n/a
 
      07-20-2006
Gary Wessle <(E-Mail Removed)> writes:

> Ian Collins <(E-Mail Removed)> writes:
>
> > Gary Wessle wrote:
> > > "Alf P. Steinbach" <(E-Mail Removed)> writes:
> > >
> > >
> > >>* 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.
 
Reply With Quote
 
John L Fjellstad
Guest
Posts: n/a
 
      07-20-2006
Gary Wessle <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C Python: Running Python code within function scope channel727272@gmail.com Python 1 09-04-2012 10:18 PM
System.Security.SecurityException: Error de solicitud de permiso de tipo System.Net.WebPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. Luis Esteban Valencia ASP .Net 0 07-14-2005 01:43 PM
System reboot/system freeze with simple code in Visual C++ 6 and lcc-win32 (OS issue?) Wagner Dias C++ 3 03-28-2005 07:59 PM
Running code when viewing aspx code =?Utf-8?B?TWF0dGhpYXMgSC4=?= ASP .Net 2 02-11-2005 02:31 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments