reading a Text-File into Vector of Strings

Discussion in 'C++' started by arnuld, Sep 13, 2007.

  1. arnuld

    arnuld Guest

    This is the partial-program i wrote, as usual, i ran into problems
    halfway:


    /* C++ Primer - 4/e
    *
    * Exercise 8.9
    * STATEMENT:
    * write a function to open a file for input and then read its
    coontents into a vector of strings, storinig each line as separate
    element in vector.
    *
    */


    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>


    /* using Pointers because 1st argument is an input file-stream and
    for 2nd argument i want to take the original vector, not the copied
    one.

    next line is the source of error, line #22 */
    void read_file( ifstream* my_file, std::vector<std::string>* svec )
    {
    ifstream infile;
    infile.open("my_file");

    /* Process File
    Here
    */
    }


    int main()
    {
    std::vector<std::string> svec;
    std::vector<std::string>* psvec;

    std::cout << "Enter the full Path to file: "; ifstream my_file;
    ifstream* p_my_file;

    std::cin >> p_my_file;

    read_file( p_my_file, psvec );

    return 0;
    }

    -------- OUTPUT --------------
    [arnuld@Arch64 c++]$ g++ -ansi -pedantic -Wall -Wextra ex_08-09.cpp
    ex_08-09.cpp:22: error: variable or field ‘read_file’ declared void
    ex_08-09.cpp:22: error: ‘ifstream’ was not declared in this scope
    ex_08-09.cpp:22: error: ‘my_file’ was not declared in this scope
    ex_08-09.cpp:22: error: expected primary-expression before ‘*’ token
    ex_08-09.cpp:22: error: 'svec' was not declared in this scope
    ex_08-09.cpp: In function 'int main()':
    ex_08-09.cpp:39: error: 'ifstream' was not declared in this scope
    ex_08-09.cpp:39: error: expected `;' before ‘my_file’
    ex_08-09.cpp:40: error: 'p_my_file' was not declared in this scope
    ex_08-09.cpp:44: error: 'read_file' was not declared in this scope
    [arnuld@Arch64 c++]$


    Opening the file is the 1st stage of the problem. 2nd stage will include
    copying the each line as a library string into the vector. I am facing
    problems at very 1st stage.

    --
    http://lispmachine.wordpress.com
    arnuld, Sep 13, 2007
    #1
    1. Advertising

  2. arnuld

    Duane Hebert Guest

    "arnuld" <> wrote in message
    news:p...
    > This is the partial-program i wrote, as usual, i ran into problems
    > halfway:
    >
    >
    > /* C++ Primer - 4/e
    > *
    > * Exercise 8.9
    > * STATEMENT:
    > * write a function to open a file for input and then read its
    > coontents into a vector of strings, storinig each line as separate
    > element in vector.
    > *
    > */
    >
    >
    > #include <iostream>
    > #include <fstream>
    > #include <vector>
    > #include <string>
    >
    >
    > /* using Pointers because 1st argument is an input file-stream and
    > for 2nd argument i want to take the original vector, not the copied
    > one.
    >
    > next line is the source of error, line #22 */
    > void read_file( ifstream* my_file, std::vector<std::string>* svec )
    > {
    > ifstream infile;
    > infile.open("my_file");
    >
    > /* Process File
    > Here
    > */
    > }


    Your function is a bit strange. If the first arg is an input
    file stream, why aren't you using it?
    You create a new ifstream and call open with a file name?
    Maybe you just want to pass the file name to the function.
    Or do you want the stream to stay open after this function
    finishes?

    Also, you don't need a pointer to your vector. Probably
    better to pass it by reference.



    >
    > int main()
    > {
    > std::vector<std::string> svec;
    > std::vector<std::string>* psvec;
    >
    > std::cout << "Enter the full Path to file: "; ifstream my_file;
    > ifstream* p_my_file;


    Shouldn't ifstream be in the std namespace?
    Duane Hebert, Sep 13, 2007
    #2
    1. Advertising

  3. On 2007-09-13 13:11, arnuld wrote:
    > This is the partial-program i wrote, as usual, i ran into problems
    > halfway:
    >
    >
    > /* C++ Primer - 4/e
    > *
    > * Exercise 8.9
    > * STATEMENT:
    > * write a function to open a file for input and then read its
    > coontents into a vector of strings, storinig each line as separate
    > element in vector.
    > *
    > */
    >
    >
    > #include <iostream>
    > #include <fstream>
    > #include <vector>
    > #include <string>
    >
    >
    > /* using Pointers because 1st argument is an input file-stream and
    > for 2nd argument i want to take the original vector, not the copied
    > one.
    >
    > next line is the source of error, line #22 */
    > void read_file( ifstream* my_file, std::vector<std::string>* svec )


    void read_file(std::istream& my_file, std::vector<std::string>& vec)

    For these kinds of things (passing the actual object and not a copy) a
    reference is preferable, unless you need to be able to pass NULL.

    > {
    > ifstream infile;
    > infile.open("my_file");


    Looking at the code in main() it looks like the file should already be
    open at this point.

    >
    > /* Process File
    > Here
    > */


    Look into using std::getline() for reading the contents of the file.

    > }
    >
    >
    > int main()
    > {
    > std::vector<std::string> svec;
    > std::vector<std::string>* psvec;
    >
    > std::cout << "Enter the full Path to file: "; ifstream my_file;
    > ifstream* p_my_file;


    Don't use a pointer, "ifstream my_file;" will do.

    >
    > std::cin >> p_my_file;


    One problem with using the >> operator here is that it will not allow
    for filenames containing spaces (I seem to recall that you can set the
    delimiter somehow to change this behaviour). I would use std::getline()
    here too. One you have the filename use my_file.open() to open the file
    (don't forget to check that it succeeded).

    > Opening the file is the 1st stage of the problem. 2nd stage will include
    > copying the each line as a library string into the vector. I am facing
    > problems at very 1st stage.


    You should probably re-read the chapter about filestreams, opening them
    are quite easy, you just use the filename as argument to the constructor
    or use the open() function:

    std::ifstream file("myfile.txt");

    or

    std::ifstream file;
    file.open("myfile.txt");

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Sep 13, 2007
    #3
  4. arnuld

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > /* using Pointers because 1st argument is an input file-stream and
    > for 2nd argument i want to take the original vector, not the copied
    > one.


    You probably don't want to use pointers here -- you almost certainly
    want to use references instead. For reference (no pun intended) you
    should virtually _always_ plan on passing any stream by reference.

    > next line is the source of error, line #22 */
    > void read_file( ifstream* my_file, std::vector<std::string>* svec )


    That needs to be 'std::ifstream'.

    > ex_08-09.cpp:22: error: variable or field ?read_file? declared void


    Without the 'std::' on the beginning, the compiler doesn't recognize
    that 'ifstream' is supposed to be a type. It's trying to read it as a
    value, so it thinks you have something like:

    int x(1);

    but it's telling you that you can't define a variable of type 'void'.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 13, 2007
    #4
  5. arnuld

    Jim Langston Guest

    "arnuld" <> wrote in message
    news:p...
    > This is the partial-program i wrote, as usual, i ran into problems
    > halfway:
    >
    >
    > /* C++ Primer - 4/e
    > *
    > * Exercise 8.9
    > * STATEMENT:
    > * write a function to open a file for input and then read its
    > coontents into a vector of strings, storinig each line as separate
    > element in vector.
    > *
    > */
    >
    >
    > #include <iostream>
    > #include <fstream>
    > #include <vector>
    > #include <string>
    >
    >
    > /* using Pointers because 1st argument is an input file-stream and
    > for 2nd argument i want to take the original vector, not the copied
    > one.
    >
    > next line is the source of error, line #22 */
    > void read_file( ifstream* my_file, std::vector<std::string>* svec )
    > {
    > ifstream infile;
    > infile.open("my_file");
    >
    > /* Process File
    > Here
    > */
    > }
    >
    >
    > int main()
    > {
    > std::vector<std::string> svec;
    > std::vector<std::string>* psvec;
    >
    > std::cout << "Enter the full Path to file: "; ifstream my_file;
    > ifstream* p_my_file;
    >
    > std::cin >> p_my_file;
    >
    > read_file( p_my_file, psvec );
    >
    > return 0;
    > }
    >
    > -------- OUTPUT --------------
    > [arnuld@Arch64 c++]$ g++ -ansi -pedantic -Wall -Wextra ex_08-09.cpp
    > ex_08-09.cpp:22: error: variable or field 'read_file' declared void
    > ex_08-09.cpp:22: error: 'ifstream' was not declared in this scope
    > ex_08-09.cpp:22: error: 'my_file' was not declared in this scope
    > ex_08-09.cpp:22: error: expected primary-expression before '*' token
    > ex_08-09.cpp:22: error: 'svec' was not declared in this scope
    > ex_08-09.cpp: In function 'int main()':
    > ex_08-09.cpp:39: error: 'ifstream' was not declared in this scope
    > ex_08-09.cpp:39: error: expected `;' before 'my_file'
    > ex_08-09.cpp:40: error: 'p_my_file' was not declared in this scope
    > ex_08-09.cpp:44: error: 'read_file' was not declared in this scope
    > [arnuld@Arch64 c++]$
    >
    >
    > Opening the file is the 1st stage of the problem. 2nd stage will include
    > copying the each line as a library string into the vector. I am facing
    > problems at very 1st stage.


    Output of the following program on my system is:
    Enter the full Path to file: c:\alcsetup.log
    [ResponseResult]
    ResultCode=0

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>

    void read_file( std::ifstream& my_file, std::vector<std::string>& svec )
    {
    std::string Line;
    while ( std::getline( my_file, Line ) )
    svec.push_back( Line );
    }

    int main()
    {
    std::vector<std::string> svec;
    std::cout << "Enter the full Path to file: ";
    std::string FileName;
    std::cin >> FileName;
    std::ifstream my_file( FileName.c_str() );
    read_file( my_file, svec );

    std::copy( svec.begin(), svec.end(),
    std::eek:stream_iterator<std::string>(std::cout, "\n") );

    return 0;
    }
    Jim Langston, Sep 13, 2007
    #5
  6. arnuld

    arnuld Guest

    > On Sep 13, 4:49 pm, Erik Wikström <> wrote:
    >> arnuld wrote:


    > void read_file(std::istream& my_file, std::vector<std::string>& vec)
    >
    > For these kinds of things (passing the actual object and not a copy) a
    > reference is preferable, unless you need to be able to pass NULL.


    ok


    > > {
    > > ifstream infile;
    > > infile.open("my_file");


    > Looking at the code in main() it looks like the file should already be
    > open at this point.


    :-\


    > > /* Process File
    > > Here
    > > */

    >
    > Look into using std::getline() for reading the contents of the file.


    got it.


    > > std::cout << "Enter the full Path to file: "; ifstream my_file;
    > > ifstream* p_my_file;

    >
    > Don't use a pointer, "ifstream my_file;" will do.


    you mean streams magically use references (stream name will be
    implicitly converted to a reference ) ?


    > > std::cin >> p_my_file;

    >
    > One problem with using the >> operator here is that it will not allow
    > for filenames containing spaces (I seem to recall that you can set the
    > delimiter somehow to change this behaviour). I would use std::getline()
    > here too. One you have the filename use my_file.open() to open the file
    > (don't forget to check that it succeeded).


    ok, done


    > You should probably re-read the chapter about filestreams, opening them
    > are quite easy, you just use the filename as argument to the constructor
    > or use the open() function:
    >
    > std::ifstream file("myfile.txt");
    >
    > or
    >
    > std::ifstream file;
    > file.open("myfile.txt");
    >


    you want to say that fstream is like a library String ?

    To manipulate the string 1st you have to create a String using:
    std::string a_string;

    looks like I have to treat an I/O Stream like a library String. I
    thought I/O streams exist automatically, as this is the place where I
    type program all the time, using Emacs.
    arnuld, Sep 13, 2007
    #6
  7. arnuld

    arnuld Guest

    > On Sep 13, 6:22 pm, Jerry Coffin <> wrote:

    > You probably don't want to use pointers here -- you almost certainly
    > want to use references instead. For reference (no pun intended) you
    > should virtually _always_ plan on passing any stream by reference.



    > ex_08-09.cpp:22: error: variable or field ?read_file? declared void
    >
    > Without the 'std::' on the beginning, the compiler doesn't recognize
    > that 'ifstream' is supposed to be a type. It's trying to read it as a
    > value, so it thinks you have something like:
    >
    > int x(1);
    >
    > but it's telling you that you can't define a variable of type 'void'.


    thanks Jerry, I learned 2 very-important things today :)
    arnuld, Sep 13, 2007
    #7
  8. On 2007-09-13 18:58, arnuld wrote:
    >> On Sep 13, 4:49 pm, Erik Wikström <> wrote:
    >>> arnuld wrote:

    >
    >> void read_file(std::istream& my_file, std::vector<std::string>& vec)
    >>
    >> For these kinds of things (passing the actual object and not a copy) a
    >> reference is preferable, unless you need to be able to pass NULL.

    >
    > ok
    >
    >
    >> > {
    >> > ifstream infile;
    >> > infile.open("my_file");

    >
    >> Looking at the code in main() it looks like the file should already be
    >> open at this point.

    >
    > :-\
    >
    >
    >> > /* Process File
    >> > Here
    >> > */

    >>
    >> Look into using std::getline() for reading the contents of the file.

    >
    > got it.
    >
    >
    >> > std::cout << "Enter the full Path to file: "; ifstream my_file;
    >> > ifstream* p_my_file;

    >>
    >> Don't use a pointer, "ifstream my_file;" will do.

    >
    > you mean streams magically use references (stream name will be
    > implicitly converted to a reference ) ?


    Not sure what you mean here. The reason you don't need a pointer is that
    the lifetime of the istream is equal to the function in which is
    declared (main()) so there's really no need to have it on the heap.
    Allocating on the heap is usually only needed if the variable have a
    lifetime incompatible with the lifetime of the scope in which it is
    declared. This way you also don't have to worry about deleting it when
    you are done with it.

    >> > std::cin >> p_my_file;

    >>
    >> One problem with using the >> operator here is that it will not allow
    >> for filenames containing spaces (I seem to recall that you can set the
    >> delimiter somehow to change this behaviour). I would use std::getline()
    >> here too. One you have the filename use my_file.open() to open the file
    >> (don't forget to check that it succeeded).

    >
    > ok, done
    >
    >
    >> You should probably re-read the chapter about filestreams, opening them
    >> are quite easy, you just use the filename as argument to the constructor
    >> or use the open() function:
    >>
    >> std::ifstream file("myfile.txt");
    >>
    >> or
    >>
    >> std::ifstream file;
    >> file.open("myfile.txt");
    >>

    >
    > you want to say that fstream is like a library String ?
    >
    > To manipulate the string 1st you have to create a String using:
    > std::string a_string;
    >
    > looks like I have to treat an I/O Stream like a library String. I
    > thought I/O streams exist automatically, as this is the place where I
    > type program all the time, using Emacs.


    ifstream is just another class in the library and have to be created
    just like any other object. The only streams that do not have to be
    created before use are std::cin, std::cout, std::cerr, and std::clog.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Sep 13, 2007
    #8
  9. arnuld

    BobR Guest

    arnuld <> wrote in message...
    > This is the partial-program i wrote, as usual, i ran into problems
    > halfway:
    >
    > /* C++ Primer - 4/e * Exercise 8.9 * STATEMENT:
    > * write a function to open a file for input and then read its
    > coontents into a vector of strings, storinig each line as separate
    > element in vector.
    > */
    >
    > #include <iostream>
    > #include <fstream>
    > #include <vector>
    > #include <string>
    >
    > /* using Pointers because 1st argument is an input file-stream and
    > for 2nd argument i want to take the original vector, not the copied
    > one. next line is the source of error, line #22 */
    > void read_file( ifstream* my_file, std::vector<std::string>* svec ){
    > ifstream infile;
    > infile.open("my_file");
    > /* Process File Here */
    > }


    void read_file( std::string const &my_file, std::vector<std::string>
    &svec ){
    std::ifstream infile( my_file.c_str() );
    if( not infile.is_open() ){ /* error */ }
    /* Process File Here */
    }

    > int main(){
    > std::vector<std::string> svec;

    // > std::vector<std::string>* psvec;
    >
    > std::cout << "Enter the full Path to file: "; ifstream my_file;

    // > ifstream* p_my_file;
    // > std::cin >> p_my_file;

    std::string p_my_file;
    std::getline( std::cin, p_my_file );

    // > read_file( p_my_file, psvec );
    read_file( p_my_file, svec );

    > return 0;
    > }


    If you really did want to do what you were thinking, don't limit your
    function.

    void read_file( std::istream &in, std::vector<std::string> &svec ){
    // note: that is 'istream', not 'ifstream'.
    for( std::string line; std::getline( in, line ); /*m_t*/ ){
    svec.push_back( line );
    } // for(line)
    return;
    } // read_file(istream&,vector<string>&)

    int main(){
    std::vector<std::string> svec;
    std::ifstream infile( "MyFile.txt" );
    std::istringstream instring( "This will \n simulate \n" ); // <sstream>

    read_file( infile, svec );
    read_file( instring, svec );
    std::cout<<"Type in some lines"<<std::endl;

    /* I'll leave it to you to figure a way to invoke/terminate
    input on this one <G>*/
    /* hint: ctrl-Z, ctrl-C, [return], as-is, ??? */
    read_file( std::cin, svec );

    std::copy( svec.begin(), svec.end(),
    std::eek:stream_iterator<std::string>( std::cout, "\n" ) );

    return 0;
    } // main()

    Don't complicate your programming with pointers, unless you are *forced* to.
    :-}

    --
    Bob R
    POVrookie
    BobR, Sep 13, 2007
    #9
  10. arnuld wrote:
    > void read_file( ifstream* my_file, std::vector<std::string>* svec )
    > {
    > ifstream infile;
    > infile.open("my_file");


    I feel from your code that you lack basic understanding of C++ types,
    pointers, and how they work and are used.

    I wonder if you even understand what "ifstream" is and what
    "ifstream*" means.

    In that function you create a new ifstream and then open it,
    suspiciously with the same name as your first function parameter, ie.
    "my_file". This makes me suspect that you think that you are opening
    that "file" represented by the first function parameter, called
    "my_file", by giving the name of that variable, as a string none the
    less, to a second ifstream.

    The variable named my_file and the string "my_file" have *nothing*
    to do with each other. They have absolutely no relation. One is a
    variable (in this case of type "pointer-to-ifstream") and the other is
    a string literal. Just because the string literal has the same
    characters as the name of that variable don't make them related in any way.

    Just the fact that you seem to try to be opening an ifstream with
    another ifstream seems to show that you don't really understand what the
    ifstream class is.

    > std::vector<std::string> svec;
    > std::vector<std::string>* psvec;


    You do understand that psvec is *not* a vector, but a pointer (and
    nothing more than a pointer), and that you are not even making it to
    point anywhere? psvec is basically an uninitialized pointer (which could
    point basically anywhere and will most probably cause a crash if you try
    to read/write it).

    svec *is* a vector, but you are not using it for anything. I'm just
    wondering what exactly is it that you expect those two lines to mean.

    If you want to have a vector (such as svec above), you don't need to
    explicitly create a pointer in order to be able to pass that vector to
    a function as a pointer (or, preferably, as a reference). There's no
    need to explicitly create a pointer just to do that.

    For example, let's assume we have somewhere a function like this:

    void foo(std::vector<std::string>& aVector) { ... }

    This takes a vector as reference. In other words, a copy of the vector
    is *not* given to this function when it's called, but instead just a
    reference to the original vector. (You can think of references as
    limited pointers.)

    Somewhere else you could write this:

    std::vector<std::string> myVector;
    foo(myVector);

    The first line creates an vector, and the second line calls the foo()
    function giving that vector as parameter. But as already seen, the
    function doesn't actually take a copy of the vector, but a reference to
    it, and thus any modification the function makes to that vector will be
    done to the 'myVector' vector.

    You could, of course, pass a pointer to your original vector too
    (although a reference is usually recommended). In that case you make the
    function to take a pointer:

    void foo(std::vector<std::string>* aVector) { ... }

    and then you can call it like this:

    std::vector<std::string> myVector;
    foo(&myVector);

    The & operator means "create a pointer pointing to this variable".
    Thus the foo() function is given a pointer to 'myVector', and if foo()
    modifies the vector behind the pointer, 'myVector' will be modified.

    In neither case you need to explicitly create a pointer variable,
    like you did. (And even if you created one, you would have to,
    naturally, make it point somewhere and not leave it uninitialized like
    you did.)

    > std::cout << "Enter the full Path to file: "; ifstream my_file;


    I'm left wondering why you put those two things in the same line.
    Was it just an oversight, or are you trying to express some relation
    between the two things? (Because there isn't, of course.)

    And again, you create an ifstream variable, and immediately after that
    (below) an uninitialized pointer of the same type. It strongly seems
    that you expect some kind of relation between them, although there is
    none. 'my_file' and 'p_my_file' are two different variables. They have
    no relation to each other. (There could be some relation if you put the
    latter to point to the former, but you didn't even do that.)

    > ifstream* p_my_file;
    >
    > std::cin >> p_my_file;


    This, again, seems to show that you don't really understand what
    ifstream is.

    First you ask for a file *name*, in other words, a *string*, and then
    you try to read that name directly into the ifstream pointer.

    An ifstream is not a string. It's a class which you can use to read a
    file. You can't ask the user for a string and somehow read it into an
    ifstream, much less into an ifstream *pointer*. (Again, a pointer is
    *not* an ifstream, it's just a pointer. Internally it's nothing more
    than a memory address. It's *not* an instance of an ifstream class.)

    What you want is to read a *string* from the user, and then use that
    to open the file. Something like this:

    std::string fileName;
    std::cin >> fileName;
    std::ifstream inputFileStream;
    inputFileStream.open(fileName.c_str());

    (The ".c_str()" part is necessary because, for whatever reason, the
    C++ standard committee decided that ifstream will not support a
    std::string directly. Anyways, it's largely irrelevant here. For now,
    just memorize this little annoyance.)

    Btw, the last two lines could be shortened to:

    std::ifstream inputFileStream(fileName.c_str());

    Reading the lines in the file into the vector can be done in a
    relatively simple way, using the std::getline() function in a loop.
    For example, something like this:

    std::vector<std::string> lines;
    std::string line;

    while(inputFileStream.good())
    {
    std::getline(inputFileStream, line);
    lines.push_back(line);
    }

    That's it. It doesn't need to be more complicated than that.
    Juha Nieminen, Sep 13, 2007
    #10
  11. arnuld

    arnuld Guest

    > On Sep 13, 11:19 pm, Juha Nieminen <> wrote:

    > I feel from your code that you lack basic understanding of C++ types,
    > pointers, and how they work and are used.
    >
    > I wonder if you even understand what "ifstream" is and what
    > "ifstream*" means.
    >
    > In that function you create a new ifstream and then open it,
    > suspiciously with the same name as your first function parameter, ie.
    > "my_file". This makes me suspect that you think that you are opening
    > that "file" represented by the first function parameter, called
    > "my_file", by giving the name of that variable, as a string none the
    > less, to a second ifstream.
    >
    > The variable named my_file and the string "my_file" have *nothing*
    > to do with each other. They have absolutely no relation. One is a
    > variable (in this case of type "pointer-to-ifstream") and the other is
    > a string literal. Just because the string literal has the same
    > characters as the name of that variable don't make them related in any way.
    >
    > Just the fact that you seem to try to be opening an ifstream with
    > another ifstream seems to show that you don't really understand what the
    > ifstream class is.


    thanks for a lengthy explanation , finally i got it :)


    > For example, let's assume we have somewhere a function like this:
    >
    > void foo(std::vector<std::string>& aVector) { ... }
    >
    > This takes a vector as reference. In other words, a copy of the vector
    > is *not* given to this function when it's called, but instead just a
    > reference to the original vector. (You can think of references as
    > limited pointers.)
    >
    > Somewhere else you could write this:
    >
    > std::vector<std::string> myVector;
    > foo(myVector);
    >
    > The first line creates an vector, and the second line calls the foo()
    > function giving that vector as parameter. But as already seen, the
    > function doesn't actually take a copy of the vector, but a reference to
    > it, and thus any modification the function makes to that vector will be
    > done to the 'myVector' vector.
    >
    > You could, of course, pass a pointer to your original vector too
    > (although a reference is usually recommended). In that case you make the
    > function to take a pointer:
    >
    > void foo(std::vector<std::string>* aVector) { ... }
    >
    > and then you can call it like this:
    >
    > std::vector<std::string> myVector;
    > foo(&myVector);
    >
    > The & operator means "create a pointer pointing to this variable".
    > Thus the foo() function is given a pointer to 'myVector', and if foo()
    > modifies the vector behind the pointer, 'myVector' will be modified.
    >
    > In neither case you need to explicitly create a pointer variable,
    > like you did. (And even if you created one, you would have to,
    > naturally, make it point somewhere and not leave it uninitialized like
    > you did.)



    I understood how to use pointer sand references.


    > std::vector<std::string> lines;
    > std::string line;
    >
    > while(inputFileStream.good())
    > {
    > std::getline(inputFileStream, line);
    > lines.push_back(line);
    >
    > }
    >
    > That's it. It doesn't need to be more complicated than that.


    exercise needs that "reading part" to be in a separate function and
    that is what i tried to did again:

    /* C++ Primer - 4/e
    *
    * Exercise 8.9
    * STATEMENT:
    * write a function to open a file for input and then read its
    * coontents into a vector of strings, storinig each line as separate
    * element in vector.
    *
    */


    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>


    void read_file( std::ifstream& my_file, std::vector<std::string>&
    svec )
    {
    std::string a_line;
    while( std::getline( my_file, a_line ))
    {
    svec.push_back( a_line );
    }

    }


    int main()
    {
    std::vector<std::string> svec;
    /* std::vector<std::string>& ref_svec = svec; */

    std::cout << "Enter the full Path to file: ";
    std::string text_file;
    std::cin >> text_file;

    /* std::ifstream infile_stream(text_file.c_str()); */

    /* line# 40, this if condition is the source of error */
    if ( std::ifstream infile_stream(text_file.c_str()) )
    {
    read_file( infile_stream, svec );
    }


    /* check the vector if it contains anything at all
    std::copy( svec.begin(), svec.end(),
    std::eek:stream_iterator<std::string>(std::cout, "\n")
    );
    */

    return 0;
    }


    ----------- OUTPUT -------------------
    ~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra ex_08-09.cpp
    ex_08-09.cpp: In function 'int main()':
    ex_08-09.cpp:40: error: expected primary-expression before
    'infile_stream'
    ex_08-09.cpp:40: error: expected `)' before 'infile_stream'
    ex_08-09.cpp:42: error: 'infile_stream' was not declared in this scope
    ~/programming/c++ $
    arnuld, Sep 14, 2007
    #11
  12. arnuld

    arnuld Guest

    > On Sep 13, 8:35 pm, "Jim Langston" <> wrote:

    > Output of the following program on my system is:
    > Enter the full Path to file: c:\alcsetup.log
    > [ResponseResult]
    > ResultCode=0
    >
    > #include <iostream>
    > #include <fstream>
    > #include <vector>
    > #include <string>
    >
    > void read_file( std::ifstream& my_file, std::vector<std::string>& svec )
    > {
    > std::string Line;
    > while ( std::getline( my_file, Line ) )
    > svec.push_back( Line );
    >
    > }
    >
    > int main()
    > {
    > std::vector<std::string> svec;
    > std::cout << "Enter the full Path to file: ";
    > std::string FileName;
    > std::cin >> FileName;
    > std::ifstream my_file( FileName.c_str() );
    > read_file( my_file, svec );
    >
    > std::copy( svec.begin(), svec.end(),
    > std::eek:stream_iterator<std::string>(std::cout, "\n") );
    >
    > return 0;
    >
    > }


    Jim, this program,as it is, never compiles on my system: gcc 4.2.1 on
    Arch x86_64 . i get this error: 'ostream_iterator' is not a member
    of 'std'

    I need to add #include <iterator> to make this program work.
    arnuld, Sep 14, 2007
    #12
  13. arnuld

    arnuld Guest

    > On Fri, 14 Sep 2007 07:09:02 +0000, arnuld wrote:

    > .... [SNIPPED].........


    > ----------- OUTPUT -------------------
    > ~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra ex_08-09.cpp
    > ex_08-09.cpp: In function 'int main()':
    > ex_08-09.cpp:40: error: expected primary-expression before
    > 'infile_stream'
    > ex_08-09.cpp:40: error: expected `)' before 'infile_stream'
    > ex_08-09.cpp:42: error: 'infile_stream' was not declared in this scope
    > ~/programming/c++ $


    OK, i got this program to work and it does what exactly the exercise wants
    and I only did this change:


    std::ifstream infile_stream( text_file.c_str() );
    /* 1st check if file was even opened or not */
    if( infile_stream.good() )
    {
    read_file( infile_stream, svec );
    }


    thanks Juha (for that ".good()", the child-class of <iostream> :)


    --
    http://lispmachine.wordpress.com
    arnuld, Sep 14, 2007
    #13
  14. arnuld wrote:
    > ~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra ex_08-09.cpp
    > ex_08-09.cpp: In function 'int main()':
    > ex_08-09.cpp:40: error: expected primary-expression before
    > 'infile_stream'
    > ex_08-09.cpp:40: error: expected `)' before 'infile_stream'
    > ex_08-09.cpp:42: error: 'infile_stream' was not declared in this scope
    > ~/programming/c++ $


    Even though the error messages are cryptic, try to examine the
    line where the first error happens (the line number is given in the
    error message). That's usually a very good hint of what's wrong.

    That's the most basic method of removing errors: Examine the
    *first* error message and try to resolve why it's happening, and
    fix it (usually it's quite clear why it's happening). Don't worry
    about the rest of the error messages. Recompile. If there are still
    errors, reiterate the process.
    Juha Nieminen, Sep 14, 2007
    #14
  15. On 2007-09-14 09:51, arnuld wrote:
    >> On Fri, 14 Sep 2007 07:09:02 +0000, arnuld wrote:

    >
    >> .... [SNIPPED].........

    >
    >> ----------- OUTPUT -------------------
    >> ~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra ex_08-09.cpp
    >> ex_08-09.cpp: In function 'int main()':
    >> ex_08-09.cpp:40: error: expected primary-expression before
    >> 'infile_stream'
    >> ex_08-09.cpp:40: error: expected `)' before 'infile_stream'
    >> ex_08-09.cpp:42: error: 'infile_stream' was not declared in this scope
    >> ~/programming/c++ $

    >
    > OK, i got this program to work and it does what exactly the exercise wants
    > and I only did this change:
    >
    >
    > std::ifstream infile_stream( text_file.c_str() );
    > /* 1st check if file was even opened or not */
    > if( infile_stream.good() )
    > {
    > read_file( infile_stream, svec );
    > }
    >
    >
    > thanks Juha (for that ".good()", the child-class of <iostream> :)


    good() is a member function of the stream, not a class.

    PS. Your program does not work correctly with a file named "my file.txt"

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Sep 14, 2007
    #15
  16. arnuld

    arnuld Guest

    > On Sep 14, 2:06 pm, Erik Wikström <> wrote:

    > good() is a member function of the stream, not a class.


    :-\


    > PS. Your program does not work correctly with a file named "my file.txt"


    fixed

    std::string text_file;
    //std::cin >> text_file;
    std::getline( std::cin, text_file );

    just changed the input method :)
    arnuld, Sep 14, 2007
    #16
  17. arnuld

    Jim Langston Guest

    "arnuld" <> wrote in message
    news:...
    >> On Sep 13, 8:35 pm, "Jim Langston" <> wrote:

    >
    >> Output of the following program on my system is:
    >> Enter the full Path to file: c:\alcsetup.log
    >> [ResponseResult]
    >> ResultCode=0
    >>
    >> #include <iostream>
    >> #include <fstream>
    >> #include <vector>
    >> #include <string>
    >>
    >> void read_file( std::ifstream& my_file, std::vector<std::string>& svec )
    >> {
    >> std::string Line;
    >> while ( std::getline( my_file, Line ) )
    >> svec.push_back( Line );
    >>
    >> }
    >>
    >> int main()
    >> {
    >> std::vector<std::string> svec;
    >> std::cout << "Enter the full Path to file: ";
    >> std::string FileName;
    >> std::cin >> FileName;
    >> std::ifstream my_file( FileName.c_str() );
    >> read_file( my_file, svec );
    >>
    >> std::copy( svec.begin(), svec.end(),
    >> std::eek:stream_iterator<std::string>(std::cout, "\n") );
    >>
    >> return 0;
    >>
    >> }

    >
    > Jim, this program,as it is, never compiles on my system: gcc 4.2.1 on
    > Arch x86_64 . i get this error: 'ostream_iterator' is not a member
    > of 'std'
    >
    > I need to add #include <iterator> to make this program work.


    Hmm.. all I can say is most likely one of my includes must include
    <iterator> because it compiled on my compiler. But, yes, it can be a bit of
    a pain to show code that won't compile on all implementations. I'll try to
    remember to #include <iterator> in the future.
    Jim Langston, Sep 14, 2007
    #17
    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. Row
    Replies:
    0
    Views:
    469
  2. pmatos
    Replies:
    6
    Views:
    23,730
  3. Replies:
    8
    Views:
    1,893
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    544
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    349
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page