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.
  • 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 ;)

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

        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
        • 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
          #13

          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
          • 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