* 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?