Thread: How to edit a .txt file in C++

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    How to edit a .txt file in C++

    Hi all, i want to edit a txt file, look the content:

    name:john
    fone:4454

    to

    name:john
    fone:8888

    i can create and output(print) the content of txt file using fstream library but can't edit the text right after the ' fone: '. Anybody can help me.

    Is the txt the best way(easier) to output data? better than xml or dat?

    thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Open the file for reading
    Open another file for writing
    In a loop,
    - read each line
    - decide whether to keep, change or delete it
    - write the (possibly changed) line to the output file.

    When you're done, delete the old file and rename the new file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Quote Originally Posted by Salem View Post
    Open the file for reading
    Open another file for writing
    In a loop,
    - read each line
    - decide whether to keep, change or delete it
    - write the (possibly changed) line to the output file.

    When you're done, delete the old file and rename the new file.
    thanks Salem, will try to put this to work.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Well YOu want the complete programme.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Quote Originally Posted by crabypatty View Post
    Well YOu want the complete programme.
    I "just" need the method:

    void edit(){

    open file.txt
    find and replace string
    save file.txt

    }


    look what i have:
    Code:
    void insert(){
    open
    insert into txt
    close
    }
    
    void read(){
    open 
    read txt
    close
    }

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    what i found:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    ifstream myfile_in ("Lista1.txt");
    ofstream myfile_out ("Lista2.txt");// problem1
    string line;
    
    void find_and_replace( string &source,  string find, string replace ) { // problem2
    
        size_t j;
    
        for ( ; (j = source.find( find )) != string::npos ; ) {
            source.replace( j, find.length(), replace );
            }
    
            myfile_out << source <<endl;
            cout << source << endl;
            }
    
    int main () {
    
      if (myfile_in.is_open())
      {
        int i = 0;
        string strcomma ;
        string strspace ;
    
        while (! myfile_in.eof() )
        {
    
          getline (myfile_in,line);
    
          strcomma = "john";
          strspace = "mariah";
    
          find_and_replace( line , strcomma , strspace );
    
          i++;
    
        }
    
        myfile_in.close();
    
      }
    
      else cout << "Unable to open file(s) ";
    
      system("PAUSE");
      return 0;
    }
    i have 2 problem "yet"
    1) the file doesn't overwrite
    2)this code MUST be a method, when i try to put inside edit(){...} i have a problem with the method 'void find_and_replace'

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As mentioned, once you have finished writing the new file, you have to rename the new file to take the old one's place.

    I have no idea what you're asking about on #2.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    problem 2:
    "must be a method" means, if I'm reading your text correctly, that is should be a class member function. Your code doesn't seem to have any classes in it, so what class is it supposed to be a member of.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > what i found:
    Shouldn't that be "what I wrote" ?
    Or is this the product of googling, or nagging elsewhere on the net?

    > ifstream myfile_in ("Lista1.txt");
    > ofstream myfile_out ("Lista2.txt");// problem1
    > string line;
    A method would imply that these global variables are in fact members of some 'class editor' instance.

    > while (! myfile_in.eof() )
    See the FAQ on why using eof() to control a loop is the wrong thing to do.

    > 1) the file doesn't overwrite
    That would be the "rename" step I mentioned earlier.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Perhaps edit() is BEFORE the find_and_replace, and since you don't have a prototype, that's what happens.

    If you use find_and_replace definition in edit, you need to define it before that, above it.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

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, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM