C++ file handling
-
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.
-
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.
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. -
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.
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 useseekp()
to move to the location returned by the previoustellp()
. Don't forget to initialize the variable you use for the result oftellp()
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 useseekp()
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, thenseekp(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
-
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.
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.