answersLogoWhite

0

There are several ways, depending on whether you wish to edit the file directly or load the file into memory to perform the edits. Editing the file directly is seldom recommended -- a better alternative is to make a temporary copy of the file, splitting the file at the insertion point to create two temporaries. Append the inserted text to the first temporary, then append the second temporary. Finally, delete the original file and rename your temporary.

You pretty much do the same thing in memory, except you first create an array to store the entire file plus the inserted text, read the first portion of the file into the array up to the insertion point, then insert your new line before reading the remainder of the file. Then write the array back to a new file. Delete the original file then rename the copy.

The best method, of course, is to read the entire file into memory first, then perform as many insertions as required before writing back to disk. But always write a copy -- never the original file. Once the copy is made, you can safely delete the original and rename the copy. This ensures the original file doesn't become corrupt if something goes wrong during the save.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Is it possible to skip to a new line when reading from a file in c plus plus?

To skip to a new line when reading from a file, assuming you are using a sequentially organized file, the usual case, you need to read and discard characters until you encounter the end-of-line character.


How do you save a radian value in c plus plus?

You can write it into a file as a floating-point value.


C plus plus reading and writing a file?

Use an input file stream (ifstream) to read from a file and an output file stream (ofstream) to write to a file. Both can be found in the <fstream> standard library header.


Is it possible to write a C plus plus or java program to draw an activity flow diagram given the sequence as a text file input and store the diagram in an output file?

Yes, this is possible.


How do you add a new library for Dev C plus plus?

If you want to know about adding new header files, then it is simple. Write your functions in a file. Save that file with extension .h in the include directry. Now, you can include this file using the #include directive


Example for w plus mode in c?

1. open the file: fopen (name, "w+") 2. write into it 3. rewind 4. read from the file


How Can you access Parallel Port using c plus plus?

You cannot directly access the parallel port because the operating system is managing that device. Use the file system. The name of the parallel port is "lpt1:". Open that as an ordinary file for write, write to it, and you will be writing on the parallel port.


How to verify the ntfs and folder permissions?

NTFS File Permission Allows the User To Full Control Change permissions and take ownership, plus perform the actions permitted by all other NTFS file permissions Modify Modify and delete the file plus perform the actions permitted by the Write permission and the Read & Execute permission Read & Execute Run applications plus perform the actions permitted by the Read permission Read Read the file, and view file attributes, ownership, and permissions Write Overwrite the file, change file attributes, and view file ownership and permissions


How do I write a C plus plus program to store my friends name in a file?

To write a C++ program that stores a friend's name in a file, you can use the <fstream> library. First, include the library and create an ofstream object to open a file for writing. Use the << operator to write the friend's name to the file, then close the file. Here's a simple example: #include <iostream> #include <fstream> using namespace std; int main() { ofstream outFile("friends.txt"); if (outFile.is_open()) { outFile << "Friend's Name\n"; // Replace with the actual name outFile.close(); } else { cout << "Unable to open file"; } return 0; }


Is there a file concept in c plus plus?

No. The standard does not define nor require a file concept.


What is 24 plus 48?

Write down 24 and directly below that 48. Then draw a short line under 48, and a plus sign right next to the line. Now, starting from right to left, take the 4 from 24 and the 8 from 48. Add these two together. 4 plus 8 makes 12. Write down the 2 from 12 directly under the line under the 8 from 48. Then write down the 1 from 12 above the 2 from 24. Now, take the second column of numers: the 1 from 12, the 2 from 24, and the 4 from 48. Add these up. 1 plus 2 plus 4 makes 7. Write down the 7 under the line under the 4 from 48, just left of the 2 from the 12. As you can see, 72 is the correct answer. 24 plus 48 makes 72.


What is the c plus plus command to read a line?

You can use cin which located in iostream.h You have to use certain data type to read string, for instance, array of char