How to write to middle of a file in C++?

I think this should be quite simple, but my googling didn't help so far... I need to write to an existing file in C++, but not necessarily at the end of the file.

I know that when I just want to append text to my file, I can pass the flag ios:app when calling open on my stream object. However, this only let's me write to the very end of the file, but not into its middle.

I made a short program to illustrate the issue:

#include <iostream> #include <fstream> using namespace std; int main () { string path = "../test.csv"; fstream file;; // ios::in and ios::out by default const int rows = 100; for (int i = 0; i < rows; i++) { file << i << "\n"; } string line; while (getline(file, line)) { cout << "line: " << line << endl; // here I would like to append more text to certain rows } file.close(); }

-------------Problems Reply------------

You cannot insert in the middle of the file. You have to copy old file to a new file and insert whatever you want to be in middle during copying to new file.

Otherwise if you intend to overwrite data/lines in existing file that is possible by using std::ostream::seekp() to identify the position within the file.

Category:c# Views:2 Time:2018-10-10

Related post

  • How to write java program to increase file limit using ulimit 2010-05-19

    I am using Fedora linux where ulimit -n 10000 increases file limit upto 10000. I want to achieve the same using java program How to write java program to increase file limit using ulimit I have tried with the below program but it didnot work well. Th

  • How to write database value in text file on defined format 2010-07-20

    I read a delimited text file into dataset. i am writing these dataset values line by line in another text file. my C# syntax is : try { DataClasses1DataContext db = new DataClasses1DataContext(); var r = from p in db.Products select p; //DataTable Ta

  • How to write a list to a file with newlines in Python3 2011-08-21

    I'm trying to write an array (list?) to a text file using Python 3. Currently I have: def save_to_file(*text): with open('/path/to/filename.txt', mode='wt', encoding='utf-8') as myfile: for lines in text: print(lines, file = myfile) myfile.close This

  • How to write (big) XML to a file in C#? 2009-05-16

    Folks, Please, what's a good way of writing really big XML documents (upto say 500 MB) in C# .NET 3.5? I've had a bit of search around, and can't seem to find anything which addresses this specific question. My previous thread (http://stackoverflow.c

  • How to write binary data to a file so that it can be read fast back [gcc/C++]? 2009-06-19

    I need to write a file format that writes out data to a file and can read it back. It should be able to read the data back fairly fast, which should involve blitting a large block of data into a std::vector (since their storage is always implemented

  • How to write frames to a video file? 2009-08-19

    I am currently writing an application that read frames from camera, modify them, and save them into a video file. I'm planing to do it with ffmpeg. There's rarely a documentation about ffmpeg. I can't find a way. Does any know how to do it? I need it

  • How to write php+html in one file more beautiful? 2010-05-08

    <? if($id == 2) { ?> html goes here <? } else { ?> if id is not 2 then something goes here <? } ?> How can I write php+html in one file more beautiful without learning smarty? --------------Solutions------------- You can use PHP's a

  • How to write content in a text file at the end of each line 2010-05-25

    I am having one text file which contain following kind of data. "3P","Smith","Richard","3 Point Promotions","3P","[email protected]","IDA","Yes",,,,0,4,5,83.33,10, "A1","Ernest","Amy","TAKE 1 Promotional Products","LCOOK","[email protected]","IDA

  • PHP: How to write code to play video files (not flash) but completely turn off the video download 2010-06-06

    PHP: How to write code in PHP to play video files (not flash) but completely turn off the video download Yahoo sites do it so far I saw --------------Solutions------------- It's not possible via PHP AFAIK. You have to use JavaScript or HTML5 tag. The

  • How to write dynamically allocated structure to file 2010-10-28

    I have a complex structure in a C program which has many members that are allocated memory, dynamically. How do I write this structure to a text / binary file? How will I be able to recreate the entire structure from the data read from the file. stru

  • How to write HTTP request content to file Asynchronously? 2010-12-30

    I am passing some data in HTTP body using the Ajax request. These contents on server are accessible using Request.InputStream. Now i want to write it to file on server disk Asynchronously. How to do that ? If the data sent using the HTTP is more then

  • how to write the last line of file 2011-03-20

    I have a file data.txt. data.txt contains text line by line as: one two three six Here I need to write data in file as: one two three four five six I dont know how to write file like this!! --------------Solutions------------- Generally, you have to

  • How to write data to a text file in C# without overwriting the current data 2011-04-01

    Greetings, I can't seem to figure out how to write data to a file without overwriting it. I know I can use File.appendtext but I am not sure how to plug that into my syntax. Here is my code: TextWriter tsw = new StreamWriter(@"C:\Hello.txt"); //Writi

  • How to write coordinate to a TIF file using libgeotiff 2011-11-16

    I'm trying to create a tif file using the GeoTiff library. I managed to write all the data into a normal tif file using the standard TIF function of the library, but I can't figure out how to use the functions to add the coordinates of the corners. I

  • How to write browser output to log file using PHP? 2012-01-18

    I have the script is which runned by Cron jobs. So, while it runs, I can't see what output of the task is. (errors and so on.) So I need to write browser output to log file using PHP. How can I do this ? --------------Solutions------------- I use thi

  • How to write rule to build object file when C files have different paths 2012-03-10

    I have bunch of C files, each with differnt path, so I have something like SRC=/path1/path2/file1.c file2.c /usr/joe/files/file3.c I want to build object file from each C file in build directory, so I did: SRCBASE=$(notdir $(SRC)) OBJS= $(addprefix $

  • In java, how to write an object to a file and later on read it from the file and convert it back to the original object in the HDFS? 2012-03-30

    I am trying to write a double[] into a file in HDFS, and later on, I will need to read it back from the file and convert it back to the double[]. Does anyone here know how to do it? Thanks, --------------Solutions------------- ObjectOutputStream An O

  • How to write a middle-tier http API endpoint that can stream results as they arrive to the client? 2012-04-04

    The scenario is this - I have a frontend web-server that I'm writing in node.js. I have an as-yet-unwritten middle-tier internal-API layer written in, well, anything. The internal-API is the only thing allowed to talk to the data-store (which happens

  • How to write comments in JSTL properties files 2012-04-18

    I'd like to know how to write comments in properties file that is used like this PF47=Ankomstdatum måste anges innan SIS kan avslutas. # AF PCT # is supposed to mean a comment but when I render the messages with fmt format JSTL tag output includes th

  • Quickfixn : How to write execution report to store file‏ 2014-01-31

    I have created a application to send messages between client-server and I am having a problem on server side. I want to write the incoming NewOrderSingle message( .body extension file) files in store folder on server side. The newordersingle message

  • How to write or append to text file using sql? 2010-05-12

    i need to call sqlcmd from stored procedure to export table to text file? --------------Solutions------------- by the following way you can do this exec master..xp_cmdshell 'echo pranay test application > d:\file.txt' more about this : http://blog

  • How to write xml data into multiple files by checking file size 2010-11-11

    I'm creating a xml sitemap! There is a limit of 50k urls or 10MB size limit. Is there a way to check the size of file and write to a new xml file when a size limit of 10MB is reached? I'm using XmlTextWriter(C#) to create xml files --------------Solu

  • How to write textbox values to .txt file with 2011-02-15

    I am new to I have a simple form with two textboxes, I want Textbox1 to write to a file named C:\VALUE1.txt and Textbox2 to write its value to a file named C:\VALUE2.txt Any text that is already in the text file MUST be over written. ---------

  • how to write finding output to same file using awk command 2011-11-05

    awk '/^nameserver/ && !modif { printf("nameserver\n"); modif=1 } {print}' testfile.txt It is displaying output but I want to write the output to same file. In my example testfile.txt. --------------Solutions------------- Not possibl

  • How to write binary data into a file without invoking implementation-defined behavior? 2012-09-17

    I am writing a program like this to write some bytes into a file. #include <fstream> int main() { char buffer[4] = {0, 0, 255, 255}; std::ofstream f("foo.txt", std::ios_base::binary); f.write(buffer, sizeof buffer); f.close(); return 0; } This

Copyright (C), All Rights Reserved.

processed in 0.118 (s). 11 q(s)