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 handling

C++ file handling

Scheduled Pinned Locked Moved C / C++ / MFC
c++learning
4 Posts 4 Posters 4 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.
  • S Offline
    S Offline
    Saboor Sarfraz
    wrote on last edited by
    #1

    Hey. i wanted to ask that i was using the escape sequence \r to write data in files but instead of moving the cursor to the beginning of the current line it moves the cursor to the next line when. However in consloe window it works just fine but in files it functions like \n. As a beginner i am not allowed to use advance stuff like pointers, structure or strings etc etc. So what could be the simplest scenario to move the cursor to the beginning of the current line in a file for the purpose of overwriting the existing txt. ofstream out("Records.txt" ); out << "This is some text.\rOverwrite"; Output: This is some text. Overwrite Desired Output: Overwriteome Text.

    Greg UtasG K J 3 Replies Last reply
    0
    • S Saboor Sarfraz

      Hey. i wanted to ask that i was using the escape sequence \r to write data in files but instead of moving the cursor to the beginning of the current line it moves the cursor to the next line when. However in consloe window it works just fine but in files it functions like \n. As a beginner i am not allowed to use advance stuff like pointers, structure or strings etc etc. So what could be the simplest scenario to move the cursor to the beginning of the current line in a file for the purpose of overwriting the existing txt. ofstream out("Records.txt" ); out << "This is some text.\rOverwrite"; Output: This is some text. Overwrite Desired Output: Overwriteome Text.

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      I hope this helps. It's from the site that I almost always consult for for C++/STL details: std::basic_ostream<CharT,Traits>::seekp - cppreference.com[^] seekp lets you reposition the "cursor" in a read-write file (ofstream) so you can overwrite characters starting at that point. But if your replacement isn't the same size as what you want to overwrite, you'll have to move the rest of the file up (if the replacement is shorter) or down (if the replacement is longer, in which case you have to do this first). It's usually easier to simply create a new file instead manipulating the original. This also preserves the original in case your software misbehaves. :)

      Robust Services Core | Software Techniques for Lemmings | Articles
      The fox knows many things, but the hedgehog knows one big thing.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      1 Reply Last reply
      0
      • S Saboor Sarfraz

        Hey. i wanted to ask that i was using the escape sequence \r to write data in files but instead of moving the cursor to the beginning of the current line it moves the cursor to the next line when. However in consloe window it works just fine but in files it functions like \n. As a beginner i am not allowed to use advance stuff like pointers, structure or strings etc etc. So what could be the simplest scenario to move the cursor to the beginning of the current line in a file for the purpose of overwriting the existing txt. ofstream out("Records.txt" ); out << "This is some text.\rOverwrite"; Output: This is some text. Overwrite Desired Output: Overwriteome Text.

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        It's just not that simple. A file is essentially an array of bytes on disc, which are written sequentially. There is no structure or other information, such as start of line, or record size or anything like that associated with a file_(1)_. When you write a Carriage Return ('\r' hex 0D) to the file, it just appends a CR character at the current location in the file. Exactly what you need to do, depends on what your exercise is. If you've got a single string of text as shown above, you'll need to examine each character and decide what to do when you find either a '\n' (start a new line) or a '\r' (move to the beginning of the current line). When you find a newline character, you'll need to write that to the file, and use tellp() to record the current location. When you find a CR ('\r') you'll want to then use seekp() to move to the location returned by the previous tellp(). Don't forget to initialize the variable you use for the result of tellp() when you open the file. If, on the other hand, you have pairs of input, e.g an initial string ("This is some text") and then the text to overwrite ("Overwrite") then its a bit simpler. You still need to record your current location in the file, but you won't need to examine each character for a CR or NL. You just need to record where you are in the file before you write the line, then use seekp() to move back there when you get the next line of input. In both cases, you may need to think about what happens when you have multiple lines of input. Are you expected to then move to then end of the current line and continue on, or continue writing at the current file cursor location. If you know you will only have a single line of text, followed by a single line to overwrite with, things are simpler still. In this case, you just need to write the first line of text, then seekp(0) to the beginning of the file, and write the second line of text. Note: (1) This might not be true for all OS's out there. But if you're using Windows, Linux or OS-X, then that is the case.

        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

        1 Reply Last reply
        0
        • S Saboor Sarfraz

          Hey. i wanted to ask that i was using the escape sequence \r to write data in files but instead of moving the cursor to the beginning of the current line it moves the cursor to the next line when. However in consloe window it works just fine but in files it functions like \n. As a beginner i am not allowed to use advance stuff like pointers, structure or strings etc etc. So what could be the simplest scenario to move the cursor to the beginning of the current line in a file for the purpose of overwriting the existing txt. ofstream out("Records.txt" ); out << "This is some text.\rOverwrite"; Output: This is some text. Overwrite Desired Output: Overwriteome Text.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Saboor Sarfraz wrote:

          but instead of moving the cursor to the beginning of the current line it moves the cursor to the next line

          Nope. A 'file' never ever does that. Rather you are attempting to display the file using something else. And that something else, not the file, is doing that.

          Saboor Sarfraz wrote:

          for the purpose of overwriting the existing txt.

          Your code is absolutely correct. For what you state the purpose is. So the problem is, as I stated above. If you are on a windows machine you can open a Powershell console or a 'cmd' console. They are NOT the same. Presumably you are using one of those then running your exe. So try doing it with the other one. If perhaps you are using an Apple Mac then you are perhaps out of luck since it always interprets the '\r' in the way you are seeing it behave.

          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