inserting text in file

This is a discussion on inserting text in file within the C++ Programming forums, part of the General Programming Boards category; Hi I want to insert new info at a specific line in a file. But I want to also delete ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    inserting text in file

    Hi I want to insert new info at a specific line in a file. But I want to also delete the previous info on that line. However I've only been able to put in the new info not overwrite the previous. How can I easily accomplish this?



    Code:
     #include <iostream> // for std::cout
    #include <fstream>
    #include <iomanip>
    #include <stdlib.h>
    #include <cstdio> 
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    fstream dataflow;
    int i =0;
    int value=19;
    string item;
    
    dataflow.open("print.txt", ios::in | ios::out);
    
    
     
    
    
    for (i=0;i<10;i++)
    {
    
     dataflow << i << std::endl;
    
    }
    
    
    
    
    dataflow.seekp(0, ios::beg); 
    
    
    
    
    dataflow << value;
    
    cout << value << endl;
    
     return 0;
    }

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    make a new file. copy up to the line you want to replace, then write the new line, then copy over the rest of the original file after the "overwritten" line.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Quote Originally Posted by m37h0d View Post
    make a new file. copy up to the line you want to replace, then write the new line, then copy over the rest of the original file after the "overwritten" line.
    the file is pretty big and I have to change several billion lines. Is there any more precise method?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Quote Originally Posted by Jarwulf View Post
    the file is pretty big and I have to change several billion lines.
    About 931 MB worth of data (10^12)? Wow.

    Is there any more precise method?
    No. Create a new file and copy data or "push back" the data.
    You can do this be copying data from say, position X to position X + 10. Then write new data between X and X + 10.
    No other way.
    That is why we typically have databases today.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    use a database.

  6. #6
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    If you have a set line width, ie, every line is exactly 80 characters, you could easily overwrite specific chunks of data using random access binary operations.
    Programming Your Mom. http://www.dandongs.com/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21