Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ifstream operator>>

ifstream operator>>

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiontutorial
20 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #1

    Hello ! I have a little question regarding the >> operator of the ifstream. I overloaded the operator so that it will support streaming of my own classes but I want to secure this process by throwing an exception when an error occured during reading the file. For example, if we try to read when the end of the file is reached (so no more data to read), what happen with the ifstream class ? Does it throw an exception ? Or do I need to check for that before each read operation ? (This will be quite big because the classes I want to save are quite big)... Any help greatly appreciated :)

    N T 2 Replies Last reply
    0
    • C Cedric Moonen

      Hello ! I have a little question regarding the >> operator of the ifstream. I overloaded the operator so that it will support streaming of my own classes but I want to secure this process by throwing an exception when an error occured during reading the file. For example, if we try to read when the end of the file is reached (so no more data to read), what happen with the ifstream class ? Does it throw an exception ? Or do I need to check for that before each read operation ? (This will be quite big because the classes I want to save are quite big)... Any help greatly appreciated :)

      N Offline
      N Offline
      NewbieStats
      wrote on last edited by
      #2

      char idata[256]; ifstream a_file ( "data/text.dat" ); while (! a_file.eof() ) { a_file.getline(idata,256); cout<< idata < At the end of the file it stops reading and continues with ur code... is that what u mean? /* Just a Human Trying to Live in a Computers World. */

      C 1 Reply Last reply
      0
      • C Cedric Moonen

        Hello ! I have a little question regarding the >> operator of the ifstream. I overloaded the operator so that it will support streaming of my own classes but I want to secure this process by throwing an exception when an error occured during reading the file. For example, if we try to read when the end of the file is reached (so no more data to read), what happen with the ifstream class ? Does it throw an exception ? Or do I need to check for that before each read operation ? (This will be quite big because the classes I want to save are quite big)... Any help greatly appreciated :)

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        it doesn't throw an exception, but it raises a flag you can test with istream::eof()


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

        1 Reply Last reply
        0
        • N NewbieStats

          char idata[256]; ifstream a_file ( "data/text.dat" ); while (! a_file.eof() ) { a_file.getline(idata,256); cout<< idata < At the end of the file it stops reading and continues with ur code... is that what u mean? /* Just a Human Trying to Live in a Computers World. */

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          No, I'm not using getline but operator>>. In fact, the operator is redifined to accept my own classes, something loke that:

          ifstream& operator>>(ifstream& i_stream,CMyClass MyClass)
          {
          i_stream>>MyClass.Data1;
          i_stream>>MyClass.Data2;
          ....
          ....
          return i_stream;
          }

          But inside this function, I want to throw an exception when there is an error (for example end of file). So, what happens when i_stream>> is called and the stream is at the end of the file ? Is an exception thrown or do I need to check the errors myself each time (quite a lot of code 'cause a lot of data inside CMyClass) ? Or maybe I can do it just once at the end of the function, is that a valid solution ?

          N T 2 Replies Last reply
          0
          • C Cedric Moonen

            No, I'm not using getline but operator>>. In fact, the operator is redifined to accept my own classes, something loke that:

            ifstream& operator>>(ifstream& i_stream,CMyClass MyClass)
            {
            i_stream>>MyClass.Data1;
            i_stream>>MyClass.Data2;
            ....
            ....
            return i_stream;
            }

            But inside this function, I want to throw an exception when there is an error (for example end of file). So, what happens when i_stream>> is called and the stream is at the end of the file ? Is an exception thrown or do I need to check the errors myself each time (quite a lot of code 'cause a lot of data inside CMyClass) ? Or maybe I can do it just once at the end of the function, is that a valid solution ?

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            end of file is not an error. and getline() raises eof() the same operator>>() does because it is the base class that manages it.


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

            C 1 Reply Last reply
            0
            • C Cedric Moonen

              No, I'm not using getline but operator>>. In fact, the operator is redifined to accept my own classes, something loke that:

              ifstream& operator>>(ifstream& i_stream,CMyClass MyClass)
              {
              i_stream>>MyClass.Data1;
              i_stream>>MyClass.Data2;
              ....
              ....
              return i_stream;
              }

              But inside this function, I want to throw an exception when there is an error (for example end of file). So, what happens when i_stream>> is called and the stream is at the end of the file ? Is an exception thrown or do I need to check the errors myself each time (quite a lot of code 'cause a lot of data inside CMyClass) ? Or maybe I can do it just once at the end of the function, is that a valid solution ?

              N Offline
              N Offline
              NewbieStats
              wrote on last edited by
              #6

              Why are you doing it that way?... The Golden Rule in Programming: Keep It Simple Stupid. No offense, but its true... The code i jus posted will do everything ur doing, and make ur life alot easier... /* Just a Human Trying to Live in a Computers World. */

              C 1 Reply Last reply
              0
              • N NewbieStats

                Why are you doing it that way?... The Golden Rule in Programming: Keep It Simple Stupid. No offense, but its true... The code i jus posted will do everything ur doing, and make ur life alot easier... /* Just a Human Trying to Live in a Computers World. */

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                Why ? Because it is not text but binary data. And so, it would much easier after that. The code for loading a class would look like:

                ifstream is(.....);
                CMyClass Class;
                is>>Class;

                Looks much compact isn't it ;-). And this is included in a try/catch statement to know if an error occured

                N T 2 Replies Last reply
                0
                • C Cedric Moonen

                  Why ? Because it is not text but binary data. And so, it would much easier after that. The code for loading a class would look like:

                  ifstream is(.....);
                  CMyClass Class;
                  is>>Class;

                  Looks much compact isn't it ;-). And this is included in a try/catch statement to know if an error occured

                  N Offline
                  N Offline
                  NewbieStats
                  wrote on last edited by
                  #8

                  oOoh i learned something today :) cant you still jus use the eof function like ur using getline? /* Just a Human Trying to Live in a Computers World. */

                  C 1 Reply Last reply
                  0
                  • C Cedric Moonen

                    Why ? Because it is not text but binary data. And so, it would much easier after that. The code for loading a class would look like:

                    ifstream is(.....);
                    CMyClass Class;
                    is>>Class;

                    Looks much compact isn't it ;-). And this is included in a try/catch statement to know if an error occured

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    ifstream is(.....);
                    CMyClass Class;
                    do {
                    is>>Class;
                    while (!is.eof())

                    isn't is as good ...


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc]

                    C 1 Reply Last reply
                    0
                    • T toxcct

                      end of file is not an error. and getline() raises eof() the same operator>>() does because it is the base class that manages it.


                      TOXCCT >>> GEII power
                      [toxcct][VisualCalc]

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #10

                      Yes I know that end of file is not an error. But for me it should be an error if it happen before the complete class has been read :). It means that I try to load data from a file which is probably corrupted so I need to throw an exception. When is this flag raised by the way ? When the last byte of data is read or when you read past the last byte of data ? And what happens if you continue to read data even if the eof flag has been raised ? A solution could be to read everything and just check at the end of the function if the complete class has been read... Thanks for the suggestions

                      1 Reply Last reply
                      0
                      • T toxcct

                        ifstream is(.....);
                        CMyClass Class;
                        do {
                        is>>Class;
                        while (!is.eof())

                        isn't is as good ...


                        TOXCCT >>> GEII power
                        [toxcct][VisualCalc]

                        C Offline
                        C Offline
                        Cedric Moonen
                        wrote on last edited by
                        #11

                        It won't compile because operator>> doesn't accept a parameter of type CMyClass ;). Thus, the need to provide the function thus the need to add secured reading ;)

                        T N 2 Replies Last reply
                        0
                        • C Cedric Moonen

                          It won't compile because operator>> doesn't accept a parameter of type CMyClass ;). Thus, the need to provide the function thus the need to add secured reading ;)

                          N Offline
                          N Offline
                          NewbieStats
                          wrote on last edited by
                          #12

                          Disreguard my stupidity and "newbie-ness", But isnt there a way to translate Binary to text and back to Binary?... Im lost now :( /* Just a Human Trying to Live in a Computers World. */

                          C 1 Reply Last reply
                          0
                          • C Cedric Moonen

                            It won't compile because operator>> doesn't accept a parameter of type CMyClass ;). Thus, the need to provide the function thus the need to add secured reading ;)

                            T Offline
                            T Offline
                            toxcct
                            wrote on last edited by
                            #13

                            didn't you say that you overloaded th operator>>() for it can get a CMyClass parameter to read ? if you do so, it will compile, and THUS, is will work fine !!!


                            TOXCCT >>> GEII power
                            [toxcct][VisualCalc]

                            C 1 Reply Last reply
                            0
                            • N NewbieStats

                              oOoh i learned something today :) cant you still jus use the eof function like ur using getline? /* Just a Human Trying to Live in a Computers World. */

                              C Offline
                              C Offline
                              Cedric Moonen
                              wrote on last edited by
                              #14

                              Yes I could but the class CMyClass contains a lot of variable (integers, float, ...) and this is quite boring for each of these variable to have something like that:

                              ifstream& operator>>(ifstream& is,CMyClass MyClass)
                              {
                              MyClass>>data1;
                              if (is.eof())
                              {
                              CFileException exception(....);
                              throw exception;
                              }

                              MyClass>>data2;
                              if (is.eof())
                              {
                              CFileException exception(....);
                              throw exception;
                              }

                              ....
                              }

                              Doesn't look really cool isn't it ? ;) But maybe I can just check this at the end od the function...

                              1 Reply Last reply
                              0
                              • T toxcct

                                didn't you say that you overloaded th operator>>() for it can get a CMyClass parameter to read ? if you do so, it will compile, and THUS, is will work fine !!!


                                TOXCCT >>> GEII power
                                [toxcct][VisualCalc]

                                C Offline
                                C Offline
                                Cedric Moonen
                                wrote on last edited by
                                #15

                                Yes of course, but the question is not there :) The problem is not reading several CMyClass, the problem is securin the reading of ONE CMyClass :) In fact, I want to secure reading the file ! So, this is in the overloaded operator that the checking must be done, not outside. If this file is corrupted (or whatever, something wrong happens) it should throw an exception. So I want to check for end of file (and for other valid parameter) INSIDE the overloaded operator for each member variable of the class.

                                T 1 Reply Last reply
                                0
                                • N NewbieStats

                                  Disreguard my stupidity and "newbie-ness", But isnt there a way to translate Binary to text and back to Binary?... Im lost now :( /* Just a Human Trying to Live in a Computers World. */

                                  C Offline
                                  C Offline
                                  Cedric Moonen
                                  wrote on last edited by
                                  #16

                                  Yes it could be but I have nothing to do with text... This is not used for printing data to the console but only reading/saving data from/to a file. So I'm just using binary data to load integers, floating, ... values

                                  1 Reply Last reply
                                  0
                                  • C Cedric Moonen

                                    Yes of course, but the question is not there :) The problem is not reading several CMyClass, the problem is securin the reading of ONE CMyClass :) In fact, I want to secure reading the file ! So, this is in the overloaded operator that the checking must be done, not outside. If this file is corrupted (or whatever, something wrong happens) it should throw an exception. So I want to check for end of file (and for other valid parameter) INSIDE the overloaded operator for each member variable of the class.

                                    T Offline
                                    T Offline
                                    toxcct
                                    wrote on last edited by
                                    #17

                                    ok. what about :

                                    if (streamObj.eof())
                                    throw CMyEOF;
                                    else {
                                    // Other cases ...
                                    }


                                    TOXCCT >>> GEII power
                                    [toxcct][VisualCalc]

                                    C 1 Reply Last reply
                                    0
                                    • T toxcct

                                      ok. what about :

                                      if (streamObj.eof())
                                      throw CMyEOF;
                                      else {
                                      // Other cases ...
                                      }


                                      TOXCCT >>> GEII power
                                      [toxcct][VisualCalc]

                                      C Offline
                                      C Offline
                                      Cedric Moonen
                                      wrote on last edited by
                                      #18

                                      Yes, that what I wanted to do but I don't want to check for eof between each member variables (and thus adding this code between each line... a little bit heavy). I think I will read all variables (even if eof is reached in between) and only check for errors at the end of the function. So to resume, is this flag raised when the last byte of data is read or when you try to read after the last byte ? In the first case, I will throw an exception when I will read the last structure in file even if it completely read (not goooood :) ). When you try to read data after the eof flag has been raised, is the failed bit raised ? If yes, then I can just check for fail() and raise an exception when it's true.

                                      T 1 Reply Last reply
                                      0
                                      • C Cedric Moonen

                                        Yes, that what I wanted to do but I don't want to check for eof between each member variables (and thus adding this code between each line... a little bit heavy). I think I will read all variables (even if eof is reached in between) and only check for errors at the end of the function. So to resume, is this flag raised when the last byte of data is read or when you try to read after the last byte ? In the first case, I will throw an exception when I will read the last structure in file even if it completely read (not goooood :) ). When you try to read data after the eof flag has been raised, is the failed bit raised ? If yes, then I can just check for fail() and raise an exception when it's true.

                                        T Offline
                                        T Offline
                                        toxcct
                                        wrote on last edited by
                                        #19

                                        i found this in the MSDN at that[^] page :

                                        // ios_base_failure.cpp
                                        // compile with: /EHsc
                                        #include <iostream>
                                        #include <fstream>

                                        int main() {
                                        using namespace std;
                                        fstream file;
                                        file.exceptions(ios::failbit);
                                        try {
                                        file.open("rm.txt", ios_base::in);
                                        // Opens nonexistent file for reading
                                        }
                                        catch(ios_base::failure f) {
                                        cout << "Caught an exception." << endl;
                                        }
                                        }

                                        i hope this time it will help :-D:rose::cool:


                                        TOXCCT >>> GEII power
                                        [toxcct][VisualCalc]

                                        C 1 Reply Last reply
                                        0
                                        • T toxcct

                                          i found this in the MSDN at that[^] page :

                                          // ios_base_failure.cpp
                                          // compile with: /EHsc
                                          #include <iostream>
                                          #include <fstream>

                                          int main() {
                                          using namespace std;
                                          fstream file;
                                          file.exceptions(ios::failbit);
                                          try {
                                          file.open("rm.txt", ios_base::in);
                                          // Opens nonexistent file for reading
                                          }
                                          catch(ios_base::failure f) {
                                          cout << "Caught an exception." << endl;
                                          }
                                          }

                                          i hope this time it will help :-D:rose::cool:


                                          TOXCCT >>> GEII power
                                          [toxcct][VisualCalc]

                                          C Offline
                                          C Offline
                                          Cedric Moonen
                                          wrote on last edited by
                                          #20

                                          Looks interesting :)... I will look at this a little bit more in details, that could be really interesting if an exception is thrown on error ;) Nowaday, thanks for your help and time. That was an interesting conversation :)

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups