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. C++ file stream read/write[SOLVED]

C++ file stream read/write[SOLVED]

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++iosvisual-studio
5 Posts 2 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.
  • B Offline
    B Offline
    Biruk Abebe
    wrote on last edited by
    #1

    I need to open a file for both reading/writing using fstream and read each character then write that character back to the file. for example i have this code.

    fstream in("test.txt",ios::in | ios::out);
    if(!in)
        cout<<"error...";
    else
    {
        char ch;
        in.seekg(0,ios::end);
        int end=in.tellg();//get the length
    
        in.seekg(0);//get back to the start
        for(int i=0;i(in.tellg())-1);//move the pointer back to the previously read position,so i could write on it
               if(in.put(ch).fail())//write back,this also move position to the next character to be read/write
                   break;//break on error
           }
        }
    }
    

    I have a file named "test.txt" which contains "ABCD". As i understand it both put() and get() methods of the stream object move the file pointer forward(i see that by getting the return value of tellg() or tellp() functions after each get() or put() method call). My question is when i comment out the code that will seek the stream pointer to "where it is now"(in.seekg(in.tellg()), the code will result incorrect results. I don't understand why this is since tellg() is showing the correct position of the character to be read next what is the purpose of explicitly seeking to it? The incorrect result is it write to the file "ABBB" instead of "ABCD". I am using visual studio 2005.

    L 1 Reply Last reply
    0
    • B Biruk Abebe

      I need to open a file for both reading/writing using fstream and read each character then write that character back to the file. for example i have this code.

      fstream in("test.txt",ios::in | ios::out);
      if(!in)
          cout<<"error...";
      else
      {
          char ch;
          in.seekg(0,ios::end);
          int end=in.tellg();//get the length
      
          in.seekg(0);//get back to the start
          for(int i=0;i(in.tellg())-1);//move the pointer back to the previously read position,so i could write on it
                 if(in.put(ch).fail())//write back,this also move position to the next character to be read/write
                     break;//break on error
             }
          }
      }
      

      I have a file named "test.txt" which contains "ABCD". As i understand it both put() and get() methods of the stream object move the file pointer forward(i see that by getting the return value of tellg() or tellp() functions after each get() or put() method call). My question is when i comment out the code that will seek the stream pointer to "where it is now"(in.seekg(in.tellg()), the code will result incorrect results. I don't understand why this is since tellg() is showing the correct position of the character to be read next what is the purpose of explicitly seeking to it? The incorrect result is it write to the file "ABBB" instead of "ABCD". I am using visual studio 2005.

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      Read the documentation The seekp method changes the location of a stream object's file pointer for output (put or write.) In most cases, seekp also changes the location of a stream object's file pointer for input (get or read.) See that last bit of the statement. Your code assumes you are independently moving the get and put pointers. The implementation in linux depends also on stream type and I am not sure in VS2005 so read the documentation carefully. In later versions of Visual Studio they use different terms seekg => The absolute position in which to move the read pointer. seekp => The position in the stream (meaning both read and write) You also need a stream clear() after any seekp otherwise a seek to last character of a file will cause an error .. ATE flag gets set

      In vino veritas

      B 1 Reply Last reply
      0
      • L leon de boer

        Read the documentation The seekp method changes the location of a stream object's file pointer for output (put or write.) In most cases, seekp also changes the location of a stream object's file pointer for input (get or read.) See that last bit of the statement. Your code assumes you are independently moving the get and put pointers. The implementation in linux depends also on stream type and I am not sure in VS2005 so read the documentation carefully. In later versions of Visual Studio they use different terms seekg => The absolute position in which to move the read pointer. seekp => The position in the stream (meaning both read and write) You also need a stream clear() after any seekp otherwise a seek to last character of a file will cause an error .. ATE flag gets set

        In vino veritas

        B Offline
        B Offline
        Biruk Abebe
        wrote on last edited by
        #3

        Thanks for your replay. But the code doesn't assume the input and output pointers are independently moved. In fact,in this case, the put() method will move the input pointer and the get() method move the output pointer as it is evident by looking at the values returned from tellp() and tellg(). This is why i am confused. if the put() statement in the code moves the input file pointer to the next character( which will be read by the next iteration) why do i need to seek to it explicitly is the question.

        L 1 Reply Last reply
        0
        • B Biruk Abebe

          Thanks for your replay. But the code doesn't assume the input and output pointers are independently moved. In fact,in this case, the put() method will move the input pointer and the get() method move the output pointer as it is evident by looking at the values returned from tellp() and tellg(). This is why i am confused. if the put() statement in the code moves the input file pointer to the next character( which will be read by the next iteration) why do i need to seek to it explicitly is the question.

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          First you have the input and output pointers mixed in the above Second the put function doesn't work the way you think in the above MSDN => The member function put (which writes a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in that stream's intermediate buffer. Put in.sync() after the put MSDN => If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the program. I think you need to do some more reading on streams with the sync it will do what you expect.

          In vino veritas

          B 1 Reply Last reply
          0
          • L leon de boer

            First you have the input and output pointers mixed in the above Second the put function doesn't work the way you think in the above MSDN => The member function put (which writes a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in that stream's intermediate buffer. Put in.sync() after the put MSDN => If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the program. I think you need to do some more reading on streams with the sync it will do what you expect.

            In vino veritas

            B Offline
            B Offline
            Biruk Abebe
            wrote on last edited by
            #5

            Thanks.

            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