C How to add characters to a file without overwriting the original file

Updated on technology 2024-03-22
14 answers
  1. Anonymous users2024-02-07

    When creating a file output stream object, add the std::ios::ate flag, and all the data is added to the end of the file when exporting.

    std::ofstream out( "", std::ios::ate | std::ios::out );

  2. Anonymous users2024-02-06

    file *f=fopen(m_filename,"a+");

    The second parameter can be set in a variety of ways, you can check out the fopen encyclopedia.

    a Open a write-only file in an append-of-the-box manner. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (EOF character reserved) a+ opens a read-write file in an additional manner.

    If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (The original EOF character is not kept) wb only writes, opens, or creates a new binary; Only data is allowed to be written. WB+ read-write opens or establishes a binary file that allows both reads and writes.

    WT+ reads and writes, opens or creates a text file; Reads and writes are allowed. AT+ Read & Write opens a text file that allows data to be read or appended to the end of the text. ab+ read-write opens a binary file, allowing data to be read or appended to the end of the file.

  3. Anonymous users2024-02-05

    file *fp;

    fp=open("filename","a"This is written in an append-of-the-way manner.

  4. Anonymous users2024-02-04

    Open an existing file by appending a write (as in the example Chinese case a), and the write will automatically add the write data to the end of the file ...... in subsequent writesHere are some examples:

  5. Anonymous users2024-02-03

    1. First, define two string variables, str1 and str2.

    2. Next, enter two strings and save them in the variables str1 and str2.

    4. Finally, output the string str1.

  6. Anonymous users2024-02-02

    There is no direct solution.

    You can first seek to the end of the file, then get the location (actually this step is to get the file length) and then seek to the file header, request the same memory space as the file size, and then read out all the contents of the file.

    Then seek to the header of the file and write the part you want to write.

    Finally, write back what you just "read out".

    Another: If you want to use the method I described, open it in ios::out mode will look at your **, do you want to make a file that only contains the original pixel color data into a bmp file? w

  7. Anonymous users2024-02-01

    Instead of inserting that function into the file, you can create a new file and write the header to the file first, and then write the contents of the second file to the new file you just created. Well, that's the only way to do it!

  8. Anonymous users2024-01-31

    Teach you a simple one.

    file *file = fopen("d:","at");

    fprintf(file, "write content test");

    fclose(file);

  9. Anonymous users2024-01-30

    The C language does not provide such library functions, so you need to write them yourself.

    You write the content of the file behind the location to be inserted into the read buffer, and then locate the file offset to insert the data before writing the readout.

    Because the files are stored continuously on disk, it is impossible to say that the content is inserted directly in the middle without overwriting the original, and the other languages provide their own writing according to the above ideas. Or unless there are some filesystems that are stored differently (but I haven't seen that).

  10. Anonymous users2024-01-29

    It depends on the form in which your file is accessed, if it is a text file: after opening the file, move the file pointer to the place where you want to insert it, cache the data in this location, and then add the new data, adding the original cached data, because the storage of normal files is a contiguous space on disk.

    If you're storing a linked list, just change the link, like a relational database.

  11. Anonymous users2024-01-28

    Example: cstring a1="ok";

    file *pfile=fopen("c:\\","a");

    fwrite(a1,1,strlen(a1),pfile);

    fclose(pfile);

    The key is"a"In this mode, the previous content will not be overwritten by automatically moving the write position to the last character before writing.

  12. Anonymous users2024-01-27

    In fact, when the program needs to write a file, you need to first determine whether the file exists, read the file once, read the file into a string or stream variable, and then read and write this variable.

  13. Anonymous users2024-01-26

    cfile file("e:\\student_",cfile::modewrite | cfile::modenotruncate);

    You can also open the file with cfile::modereadwrite, and then seektoend() to navigate to the end of the file.

  14. Anonymous users2024-01-25

    This is not possible with ordinary text files, only with structural files.

Related questions
17 answers2024-03-22

Let's talk about the idea first:

Get the last one within this string. location, which is the index. >>>More

8 answers2024-03-22

WinRAR also integrates the function of volume compression, and it does not have to be supported by floppy disks like WinZip to use this function, and can store a large file volume compression in any specified drive letter when making, so this is also greatly convenient for us to use. >>>More

23 answers2024-03-22

In fact, there are many reasons why the folders in the U disk cannot be deleted, some are due to viruses, some are because there are system files in the U disk, some are because of the reading and writing errors of the U disk data, and there are many empty folders in the U disk, and the solution is as follows (I figured it out myself, not necessarily all of them are applicable): >>>More

10 answers2024-03-22

It's produced by Tencent.,It's a Tencent game anti-plug-in driver.,Used to protect Tencent series of software,In the case of an SX error, the files generated in the system32 folder of the system folder,The file will be automatically deleted after restarting。 >>>More

10 answers2024-03-22

The meaning of the title is said upstairs.,Here's a **.。 >>>More