running a system function from c++ code

Discussion in 'C++' started by Gary Wessle, Jul 20, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    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
     
    Gary Wessle, Jul 20, 2006
    #1
    1. Advertising

  2. * 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?
     
    Alf P. Steinbach, Jul 20, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Gary Wessle Guest

    "Alf P. Steinbach" <> 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 $<
    ##########################
     
    Gary Wessle, Jul 20, 2006
    #3
  4. Gary Wessle

    Ian Collins Guest

    Gary Wessle wrote:
    > "Alf P. Steinbach" <> 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.
     
    Ian Collins, Jul 20, 2006
    #4
  5. Gary Wessle

    Gary Wessle Guest

    Gary Wessle <> writes:

    > "Alf P. Steinbach" <> 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$
     
    Gary Wessle, Jul 20, 2006
    #5
  6. Gary Wessle

    Gary Wessle Guest

    Ian Collins <> writes:

    > Gary Wessle wrote:
    > > "Alf P. Steinbach" <> 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;


    }
     
    Gary Wessle, Jul 20, 2006
    #6
  7. Gary Wessle

    Ian Collins Guest

    Gary Wessle wrote:
    > Ian Collins <> 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.
     
    Ian Collins, Jul 20, 2006
    #7
  8. * Gary Wessle:
    > "Alf P. Steinbach" <> 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?
     
    Alf P. Steinbach, Jul 20, 2006
    #8
  9. Gary Wessle

    Gary Wessle Guest

    Gary Wessle <> writes:

    > Ian Collins <> writes:
    >
    > > Gary Wessle wrote:
    > > > "Alf P. Steinbach" <> 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.
     
    Gary Wessle, Jul 20, 2006
    #9
  10. Gary Wessle <> 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
     
    John L Fjellstad, Jul 20, 2006
    #10
  11. Gary Wessle

    Kai-Uwe Bux Guest

    Gary Wessle wrote:

    > Gary Wessle <> writes:
    >
    >> Ian Collins <> writes:
    >>
    >> > Gary Wessle wrote:
    >> > > "Alf P. Steinbach" <> 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.


    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
     
    Kai-Uwe Bux, Jul 20, 2006
    #11
  12. Gary Wessle

    Gary Wessle Guest

    thanks alot
     
    Gary Wessle, Jul 20, 2006
    #12
  13. Gary Wessle

    Gary Wessle Guest

    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 $<
    ##########################
     
    Gary Wessle, Jul 20, 2006
    #13
  14. * 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?
     
    Alf P. Steinbach, Jul 20, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWF0dGhpYXMgSC4=?=

    Running code when viewing aspx code

    =?Utf-8?B?TWF0dGhpYXMgSC4=?=, Feb 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    463
    Stefano
    Feb 11, 2005
  2. Wagner Dias
    Replies:
    3
    Views:
    3,102
    Andre Kostur
    Mar 28, 2005
  3. Benjamin Rutt
    Replies:
    2
    Views:
    982
    Peter Hansen
    Dec 20, 2005
  4. Sergeant_Pepper

    Return-Code of "system" on a 64-bit system

    Sergeant_Pepper, Dec 18, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    105
    Sergeant_Pepper
    Dec 19, 2005
  5. Replies:
    1
    Views:
    288
    Terry Reedy
    Sep 4, 2012
Loading...

Share This Page