Passing an ifstream object to another function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csvka

    Passing an ifstream object to another function

    Hello,

    I wonder if I could pick your brains. I'm beginning to learn about C++. I
    have opened a file in my program and I want to read lines from it. I would
    like this to be done in a separate function called readline() because I
    would also like to do some processing on the line each time (ignoring
    comments and so on).

    I have:

    void Fileloader::rea dline(char *string, ifstream file) {
    file.getline(st ring, sizeof(string), '\n');
    }


    And in another function I have:

    ifstream file;
    file.open("myfi le.txt", ios::in);
    char string[512];
    readline(string , file);

    To call my readline file.

    This comes up with lots of errors like:
    /usr/include/c++/3.3.1/bits/ios_base.h:668: error:
    `std::ios_base: :ios_base(const std::ios_base&) ' is private
    fileloader.cpp: 62: error: within this context


    Can anyone see what I'm doing wrong and point me in the right direction?

    Thanks very much,
    Tom


  • Victor Bazarov

    #2
    Re: Passing an ifstream object to another function

    "csvka" <tom@tomdavis.o rg.uk> wrote...[color=blue]
    > I wonder if I could pick your brains. I'm beginning to learn about C++.[/color]
    I[color=blue]
    > have opened a file in my program and I want to read lines from it. I[/color]
    would[color=blue]
    > like this to be done in a separate function called readline() because I
    > would also like to do some processing on the line each time (ignoring
    > comments and so on).
    >
    > I have:
    >
    > void Fileloader::rea dline(char *string, ifstream file) {[/color]

    Make it

    voif Fileloader::rea dline(string& str, ifstream& file)
    [color=blue]
    > file.getline(st ring, sizeof(string), '\n');[/color]

    'sizeof' is not good here (even when your 'string' was a pointer).
    It never gives the size of the array behind the pointer.

    V


    Comment

    • Gianni Mariani

      #3
      Re: Passing an ifstream object to another function

      csvka wrote:[color=blue]
      > Hello,
      >
      > I wonder if I could pick your brains. I'm beginning to learn about C++. I
      > have opened a file in my program and I want to read lines from it. I would
      > like this to be done in a separate function called readline() because I
      > would also like to do some processing on the line each time (ignoring
      > comments and so on).
      >
      > I have:
      >
      > void Fileloader::rea dline(char *string, ifstream file) {[/color]

      Try passing ifstream by reference :

      void Fileloader::rea dline(char *string, ifstream & file) {
      [color=blue]
      > file.getline(st ring, sizeof(string), '\n');
      > }
      >
      >
      > And in another function I have:
      >
      > ifstream file;
      > file.open("myfi le.txt", ios::in);
      > char string[512];
      > readline(string , file);
      >
      > To call my readline file.
      >
      > This comes up with lots of errors like:
      > /usr/include/c++/3.3.1/bits/ios_base.h:668: error:
      > `std::ios_base: :ios_base(const std::ios_base&) ' is private
      > fileloader.cpp: 62: error: within this context
      >
      >
      > Can anyone see what I'm doing wrong and point me in the right direction?
      >[/color]

      You can't make copies of ifstream.

      The other thing is that you probably don't care about the "f" or file
      semantics of a stream.

      So this below is better.

      void Fileloader::rea dline(char *string, istream & file)

      But wait - there's more - sizeof( string ) will return the sizeof a
      char * (which is probably 4 on your machine) which is probably NOT what
      you want.

      You also don't really want to use char *, try and see if std::string
      works for you.





      Comment

      • Buster

        #4
        Re: Passing an ifstream object to another function


        "csvka" <tom@tomdavis.o rg.uk> wrote in message
        news:c0ojp0$c8b $1@wisteria.csv .warwick.ac.uk. ..[color=blue]
        > Hello,
        >
        > I wonder if I could pick your brains. I'm beginning to learn[/color]
        about C++. I[color=blue]
        > have opened a file in my program and I want to read lines from it.[/color]
        I would[color=blue]
        > like this to be done in a separate function called readline()[/color]
        because I[color=blue]
        > would also like to do some processing on the line each time[/color]
        (ignoring[color=blue]
        > comments and so on).
        >
        > I have:
        >
        > void Fileloader::rea dline(char *string, ifstream file) {
        > file.getline(st ring, sizeof(string), '\n');
        > }[/color]

        Here you seem to want to provide readline as an alias for getline.
        Why not just
        use getline? Is there any reason readline must be a member function?
        It doesn't use any of the calling object's data.

        But more important than that, don't use std::istream::g etline (char
        *, int, char)!
        Instead use std::getline (std::istream &, std::string &), which is
        in the standard
        <string> header. The version of getline which takes a
        character-array as a
        buffer is unsafe unless used properly (which you haven't - the
        length of the
        null-terminated sequence of characters pointed to by "string" is
        unlikely to
        be a good buffer size for getline) because the buffer can be
        overrun. A
        corollary of this advice is that when you want a string, use
        std::string, not
        character-arrays, because it handles the buffer's allocation and
        deallocation
        automatically, can be safely copied, etc.

        Taking this on board, in your program, where previously you had:
        object.readline (array, stream); // for 'object' declared as a
        Fileloader instance
        you will now want:
        std::getline (s, stream); // where s is an std::string object.
        [color=blue]
        >
        > And in another function I have:
        >
        > ifstream file;
        > file.open("myfi le.txt", ios::in);
        > char string[512];
        > readline(string , file);
        >
        > To call my readline file.
        >
        > This comes up with lots of errors like:
        > /usr/include/c++/3.3.1/bits/ios_base.h:668: error:
        > `std::ios_base: :ios_base(const std::ios_base&) ' is private
        > fileloader.cpp: 62: error: within this context[/color]

        std::ios_base is a base class of std::ifstream, and the copy
        constructor
        of std::ios_base is private, so std::ifstream objects cannot be
        copied.
        You would need to pass the stream by reference, as:

        void Fileloader::rea dline(char *string, std::ifstream & file);

        But seriously, use std::string and std::getline (std::istream &,
        std::string &)
        instead.
        [color=blue]
        > Can anyone see what I'm doing wrong and point me in the right[/color]
        direction?[color=blue]
        >
        > Thanks very much,
        > Tom[/color]

        Good luck and best regards,
        Buster.


        Comment

        • John Harrison

          #5
          Re: Passing an ifstream object to another function


          "csvka" <tom@tomdavis.o rg.uk> wrote in message
          news:c0ojp0$c8b $1@wisteria.csv .warwick.ac.uk. ..[color=blue]
          > Hello,
          >
          > I wonder if I could pick your brains. I'm beginning to learn about C++.[/color]
          I[color=blue]
          > have opened a file in my program and I want to read lines from it. I[/color]
          would[color=blue]
          > like this to be done in a separate function called readline() because I
          > would also like to do some processing on the line each time (ignoring
          > comments and so on).
          >
          > I have:
          >
          > void Fileloader::rea dline(char *string, ifstream file) {
          > file.getline(st ring, sizeof(string), '\n');
          > }[/color]

          You're just learning so you are making all the classic mistakes.

          Pass streams by reference.

          Use istream not ifstream in function. Why do you care what sort of stream it
          is, as long as you can read from it who cares. If you say ifstream you are
          saying I only want to read from a file, if you say istream you are saying I
          want to read from anything, so your code is more flexible.

          Don't use char*, use string instead. Especially don't use sizeof on char*
          because IT WONT WORK!!! (The answer is usually 4, which is the size of a
          char* pointer).

          Putting all that together we have

          void Fileloader::rea dline(string& str, istream& input) {
          getline(input, str, '\n');
          }

          which is how you should write that function.
          [color=blue]
          >
          >
          > And in another function I have:
          >
          > ifstream file;
          > file.open("myfi le.txt", ios::in);
          > char string[512];
          > readline(string , file);[/color]

          Right change this to

          ifstream file("myfile.tx t", ios::in);
          string str;
          readline(str, file);

          No need to call open, you can declare the variable and open the file in one
          go.

          But most importantly, you've replaced the char array (which was limited to
          512) with a string which is unlimited! You need to include the header
          <string> (no .h in that) to get the string class.

          You'll make life much easier for yourself learning C++ if you get into good
          habits right away. There a lot of different ways to go wrong in C++. Most
          importantly you need a good book, which one are you using?

          john



          Comment

          • Tom Davis

            #6
            Re: Passing an ifstream object to another function

            John Harrison wrote:

            [snipped]
            [color=blue]
            >
            > No need to call open, you can declare the variable and open the file in
            > one go.
            >
            > But most importantly, you've replaced the char array (which was limited to
            > 512) with a string which is unlimited! You need to include the header
            > <string> (no .h in that) to get the string class.[/color]

            Thank you. :-) That was really helpful.
            [color=blue]
            >
            > You'll make life much easier for yourself learning C++ if you get into
            > good habits right away. There a lot of different ways to go wrong in C++.
            > Most importantly you need a good book, which one are you using?[/color]


            Well, I've done a bit of Java and a bit of C before. I've picked up the
            O'Reilly book "Practical C++ Programming". Can you recommend any other
            good ones?

            Thanks again for the help,

            Tom
            (csvka in the other post)

            Comment

            • John Harrison

              #7
              Re: Passing an ifstream object to another function


              "Tom Davis" <news@NOSPAM-tomdavis-PLEASE.org.uk> wrote in message
              news:c0opj7$fd1 $1@wisteria.csv .warwick.ac.uk. ..[color=blue]
              > John Harrison wrote:
              >
              > [snipped]
              >[color=green]
              > >
              > > No need to call open, you can declare the variable and open the file in
              > > one go.
              > >
              > > But most importantly, you've replaced the char array (which was limited[/color][/color]
              to[color=blue][color=green]
              > > 512) with a string which is unlimited! You need to include the header
              > > <string> (no .h in that) to get the string class.[/color]
              >
              > Thank you. :-) That was really helpful.
              >[color=green]
              > >
              > > You'll make life much easier for yourself learning C++ if you get into
              > > good habits right away. There a lot of different ways to go wrong in[/color][/color]
              C++.[color=blue][color=green]
              > > Most importantly you need a good book, which one are you using?[/color]
              >
              >
              > Well, I've done a bit of Java and a bit of C before. I've picked up the
              > O'Reilly book "Practical C++ Programming". Can you recommend any other
              > good ones?
              >
              > Thanks again for the help,
              >
              > Tom
              > (csvka in the other post)[/color]

              If you have previously programmed before then the one that is normally
              recommended by this group is Accelerated C++ by Koenig and Moo.

              The one you are reading has a poor review here



              The author is accused of simply translating a C style into C++, since you
              are used to C that would be a problem for you. Also it cannot be denied that
              the book is fairly old. C++ has moved on a lot in recent years.

              john


              Comment

              Working...