Adding Data at beginning of file [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Delete a Line from a file in C Language
In C++, what is the proper way to insert a line at the beginning of a text file?
How can i add data at the beginning of a file using c/c++ programming?
I have tried following code :
fstream file;
stmt.open(L"d:\\xyz.txt",ios::in|ios::out|ios::app);
but this is appending at the end of file.

You cannot do that.
With only standard C or C++, if you want to do it atomically, you have to write everything to a new file (i.e. new data plus old file), and then move the file over. If you want to play it risky, you can read a block of data, and write the new content at the beginning and move the data up block by block (but if something interrupts you, you've destroyed the file).
If you have access to memory mapping, you can try a different approach: Memory-map the entire file, memmove it by the required offset, and memcpy the new data into the initial segment.

You could do this:
Create a new file
Add the new data at the top
Append the data from the old file
There's rename in cstdio but I'm sure there's also something C++-specific.

Your only options of opening a file as far as i know are read,write and append. Therefore you should read the entire file content (provided it's not a huge file), open a temp file for writing , then write what you want followed by the buffer you read from the old file.
You can also try to open the file in w+ mode and try to position the cursor at the beginning of the file but i don't know if that would work unfortunately

Related

How do I delete bytes from a file in C or C++?

I've been Googling this for hours...reading and reading and reading, and yet nothing I come across seems to answer this simple question: In C or C++ programming: I have a file, it contains "hello world". I want to delete "world" (like pressing Backspace in a text editor), then save the file. How do I do this?
I know that files are streams (excellent info on that here!), which don't seem to have a way to delete items from a file per say, and I've studied all of the file-related functions in stdio.h: http://www.cplusplus.com/reference/cstdio/fopen/.
It seems to me that files and streams therefore are NOT like arrays: I can't just delete a byte from a file! Rather (I guess?) I have to create an entire new file and copy the whole original file into the new file withOUT the parts I want to delete? Is that the case?
The only other option I can think of is to seek to the position before "world", then write binary zeros to the end of the file, thereby overwriting "world". The problem with this, however, is a text editor will now no longer properly display this file, as it has non-printable characters in it--and the file size hasn't shrunk--it still contains these bytes--it's just that they hold zeros now instead of ASCII text, so this doesn't seem to be right either.
Related
Resizing a file in C++
You want std::filesystem::resize_file()
Assume your original file is "data.txt". As part of your code, open a new temp file say "data.txt.tmp" and start writing contents to it from original file. Upon writing data, replace the original file with the new one.
You can use a memory map from the source file and copy the data blocks you want to another memory map over target file. That's the easy and fast way (see http://man7.org/linux/man-pages/man2/mmap.2.html)

Possible to store data into text file permanently in c++

all I have a one small program in c++.I need to store some text in the .txt file it stored and file also created but again i run with some other data the previous data is deleted in .txt file, help anyone how to solve that problem i ask doubt is it possible in c++ yes/no.someone help!
The behavior you are describing is called overwriting a file.
You have two choices, if the file already exists:
Append to already existing file. Read more in How to append text to a text file in C++?
Write the data to a different file.

Read and Rewrite the same file in C++

I have to read from a file, do some operations based on the data I got from that file and then rewrite the whole file with new values obtained after the operations were made. I tried
fstream file("date.in", ios::in|ios::out)
but seems like it puts the new set of data at the end of file. Also tried
fstream file("date.in", ios::in|ios::out|ios::trunc)
but then I can't even read the first set of data as it appears not to be there.
If you want to read and write to the same offsets in the file, you can set the put and get pointers with seekp() and seekg(). Their documentation can be found at cppreference.com.

How to NOT OVERWRITE but put content in the middle of a file?

I am trying to write a c++ code that will put some additional text in the middle that not overwrite. I have tried every possible combination of tags but none of them are working. Can anybody give me an working example ?
For example :-
if input is :-
Hello!
Hey are you there ?
Is anybody home ?
Then the output should be :-
Hello!
Hey are you there ?
Where are you ?
Is anybody home ?
The Where are you text is inserted in the middle.I'm using c++ file handling.
I think that files work alot like arrays in that you can't just do an easy insert. For instance if you are implementing a vector or arraylist and want to insert a value in the middle you must shit all values after that. To insert in the middle I think you will need to shift all the contents bellow. I would maybe read everything into memory first or use a temp file.
This is not a limitation of C++ but of the underlying filesystem (on most modern file-systems).
A file is a block(s) of contiguous bytes, you can not append in the middle.
You have two options:
Read the file into memory.
Manipulate the file in memory
Overwrite the old file.
Open the file for reading and a temporary file for writing.
Copy from input file to output file until you get to the point you want to add text.
Write the modifications and finish the copy.
Replace the file with the tempfile.

Remove A Line Of Text With Filestreams (C++)

I have a large text file.
Each time my program runs, it needs to read in the first line, remove it, and put that data back into the bottom of the file.
Is there a way to accomplish this task without having to read in every part of the file?
It would be great to follow this example of pseudo code:
1. Open file stream for reading/writing
2. data = first line of file
3. remove first line from file <-- can I do this?
4. Close file stream
5. Open file stream for appending
6. write data to file
7. Close file stream
The reason I'm trying to avoid reading everything in is because the program runs at a specific time each day. I don't want the delay to be longer each time the file gets bigger.
All the solutions I've found require that the program process the whole file. If C++ filestreams are not able to accomplish this, I'm up for whatever alternative is quick and efficient for my C++ program to execute.
thanks.
The unfortunate truth is that no filesystem on a modern OS is designed to do this. The only way to remove something from the beginning of a file is to copy the contents to a new file, except for the first bit. There's simply no way to do precisely what you want to do.
But hopefully you can do a bit of redesign. Maybe each entry could be a record in a database -- then the reordering can be done very efficiently. Or perhaps the file could contain fixed-size records, and you could use a second file of indexes to specify record order, so that rearranging the file was just a matter of updating the indices.