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. Writing Data to Files

Writing Data to Files

Scheduled Pinned Locked Moved C / C++ / MFC
14 Posts 5 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.
  • J JonEngle

    Order of operation precedence: hours << day << endl; is equal to hours << (day << endl); You want: hours << day; hours << endl;

    A Offline
    A Offline
    Achim Klein
    wrote on last edited by
    #3

    The associativity of << is 'left to right'. So: hours << day << endl; is equal to (hours << day) << endl; Only the associativity of unary and assignment operators is 'right to left'. You can see it here. Regards Achim Klein


    We can do no great things, only small things with great love. - Mother Theresa

    1 Reply Last reply
    0
    • J JonEngle

      Order of operation precedence: hours << day << endl; is equal to hours << (day << endl); You want: hours << day; hours << endl;

      S Offline
      S Offline
      Sweet Flame
      wrote on last edited by
      #4

      I changed it so that it reads: hours<

      A A 3 Replies Last reply
      0
      • S Sweet Flame

        I changed it so that it reads: hours<

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #5

        What's the class of your hours object ? Is it an ofstream ?

        S 1 Reply Last reply
        0
        • S Sweet Flame

          I changed it so that it reads: hours<

          A Offline
          A Offline
          Achim Klein
          wrote on last edited by
          #6

          And of which type is your day entity ? Is it just an int ?


          We can do no great things, only small things with great love. - Mother Theresa

          S 1 Reply Last reply
          0
          • S Sweet Flame

            I changed it so that it reads: hours<

            A Offline
            A Offline
            Achim Klein
            wrote on last edited by
            #7

            Can you compile this ?

            // --------
            // Main.cpp
            // --------
            /**
            * @file
            * @brief Re: main()
            * @version 0.1
            */

            // --------
            // Includes
            // --------
            #include <iostream>
            #include <fstream>

            // ---------------
            // Used namespaces
            // ---------------
            using namespace std;

            // -------------------------------
            // Definition of the MyClass class
            // -------------------------------
            /**
            * Foo.
            */
            class MyClass
            {

            public:

            // ------------
            // Construction
            // ------------
            
            /// standard-constructor
            MyClass();
            
            
            // -------------
            // Serialization
            // -------------
            
            /// writes the object to the passed stream
            ostream& write(ostream& Stream) const;
            

            private:

            // ----------
            // Attributes
            // ----------
            
            /// the first member
            int m\_first;
            
            /// the second member
            int m\_second;
            
            /// the third member
            int m\_third;
            

            };

            // -------
            // MyClass
            // -------
            /**
            * The standard-constructor.
            */
            MyClass::MyClass()
            {
            m_first = 1;
            m_second = 2;
            m_third = 3;
            }

            // -----
            // write
            // -----
            /**
            * Writes the object to the passed stream.
            */
            ostream& MyClass::write(ostream& Stream) const
            {
            return Stream << "first : " << m_first << endl
            << "second : " << m_second << endl
            << "third : " << m_third;
            }

            // ----------
            // operator<<
            // ----------
            /**
            * ostream << MyClass
            */
            ostream& operator<<(ostream& Stream, const MyClass& Object)
            {
            return Object.write(Stream);
            }

            // ----
            // main
            // ----
            /**
            * The application starts here.
            *
            * @param argc number of arguments
            * @param argv list of arguments
            *
            * @return 0 if finished successfully
            */
            int main(int argc, char** argv)
            {
            // ofstream cout("cout.txt");

            cout << MyClass() << endl;
            
            return 0;
            

            }


            We can do no great things, only small things with great love. - Mother Theresa

            S 1 Reply Last reply
            0
            • A Anonymous

              What's the class of your hours object ? Is it an ofstream ?

              S Offline
              S Offline
              Sweet Flame
              wrote on last edited by
              #8

              yes, hours is an ofstream.

              1 Reply Last reply
              0
              • A Achim Klein

                And of which type is your day entity ? Is it just an int ?


                We can do no great things, only small things with great love. - Mother Theresa

                S Offline
                S Offline
                Sweet Flame
                wrote on last edited by
                #9

                yes, my "day" is an int

                1 Reply Last reply
                0
                • A Achim Klein

                  Can you compile this ?

                  // --------
                  // Main.cpp
                  // --------
                  /**
                  * @file
                  * @brief Re: main()
                  * @version 0.1
                  */

                  // --------
                  // Includes
                  // --------
                  #include <iostream>
                  #include <fstream>

                  // ---------------
                  // Used namespaces
                  // ---------------
                  using namespace std;

                  // -------------------------------
                  // Definition of the MyClass class
                  // -------------------------------
                  /**
                  * Foo.
                  */
                  class MyClass
                  {

                  public:

                  // ------------
                  // Construction
                  // ------------
                  
                  /// standard-constructor
                  MyClass();
                  
                  
                  // -------------
                  // Serialization
                  // -------------
                  
                  /// writes the object to the passed stream
                  ostream& write(ostream& Stream) const;
                  

                  private:

                  // ----------
                  // Attributes
                  // ----------
                  
                  /// the first member
                  int m\_first;
                  
                  /// the second member
                  int m\_second;
                  
                  /// the third member
                  int m\_third;
                  

                  };

                  // -------
                  // MyClass
                  // -------
                  /**
                  * The standard-constructor.
                  */
                  MyClass::MyClass()
                  {
                  m_first = 1;
                  m_second = 2;
                  m_third = 3;
                  }

                  // -----
                  // write
                  // -----
                  /**
                  * Writes the object to the passed stream.
                  */
                  ostream& MyClass::write(ostream& Stream) const
                  {
                  return Stream << "first : " << m_first << endl
                  << "second : " << m_second << endl
                  << "third : " << m_third;
                  }

                  // ----------
                  // operator<<
                  // ----------
                  /**
                  * ostream << MyClass
                  */
                  ostream& operator<<(ostream& Stream, const MyClass& Object)
                  {
                  return Object.write(Stream);
                  }

                  // ----
                  // main
                  // ----
                  /**
                  * The application starts here.
                  *
                  * @param argc number of arguments
                  * @param argv list of arguments
                  *
                  * @return 0 if finished successfully
                  */
                  int main(int argc, char** argv)
                  {
                  // ofstream cout("cout.txt");

                  cout << MyClass() << endl;
                  
                  return 0;
                  

                  }


                  We can do no great things, only small things with great love. - Mother Theresa

                  S Offline
                  S Offline
                  Sweet Flame
                  wrote on last edited by
                  #10

                  yes, i can

                  A 1 Reply Last reply
                  0
                  • S Sweet Flame

                    yes, i can

                    A Offline
                    A Offline
                    Achim Klein
                    wrote on last edited by
                    #11

                    Maybe the compiler gets confused by your included header files. Try to use: <string> instead of <string.h> <fstream> instead of <fstream.h> <iostream> instead of <iostream.h> ... And avoid mixing them up.


                    We can do no great things, only small things with great love. - Mother Theresa

                    1 Reply Last reply
                    0
                    • S Sweet Flame

                      I am trying to write data to a file so that I can open it later. I am trying to save the values of variable to the file. After the file has been opened, I input: "hours << day << endl;" "hours" is the outfile and "day" is a variable that I am trying to write into it. If I remove the "<

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #12

                      Something else is at play here. This works fine for me:

                      #include <fstream.h>

                      void main( void )
                      {
                      ofstream hours("c:\\file.txt");
                      int day = 20;
                      hours << day << endl;
                      }


                      "Take only what you need and leave the land as you found it." - Native American Proverb

                      A 1 Reply Last reply
                      0
                      • D David Crow

                        Something else is at play here. This works fine for me:

                        #include <fstream.h>

                        void main( void )
                        {
                        ofstream hours("c:\\file.txt");
                        int day = 20;
                        hours << day << endl;
                        }


                        "Take only what you need and leave the land as you found it." - Native American Proverb

                        A Offline
                        A Offline
                        Achim Klein
                        wrote on last edited by
                        #13

                        Yes, this problem is really a bit strange... On my system the following code causes a C2679 error:

                        #include <string>
                        #include <fstream.h>

                        int main(int argc, char** argv)
                        {
                        ofstream hours("Hours.txt");

                        hours << "Hallo" << std::endl;
                        
                        return 0;
                        

                        }


                        We can do no great things, only small things with great love. - Mother Theresa

                        D 1 Reply Last reply
                        0
                        • A Achim Klein

                          Yes, this problem is really a bit strange... On my system the following code causes a C2679 error:

                          #include <string>
                          #include <fstream.h>

                          int main(int argc, char** argv)
                          {
                          ofstream hours("Hours.txt");

                          hours << "Hallo" << std::endl;
                          
                          return 0;
                          

                          }


                          We can do no great things, only small things with great love. - Mother Theresa

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #14

                          Writing string is different than writing int (your original problem). The ofstream class does not support writing string data. You have to provide that yourself:

                          #include <fstream>
                          std::ofstream &operator<<( std::ofstream &os, const std::string &str )
                          {
                          os << str.c_str();
                          return os;
                          }


                          "Take only what you need and leave the land as you found it." - Native American Proverb

                          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