Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    writing and appending to file lots of times takes to long how to improve ?

    Hello all

    im using ofstream to writing to file and then appending strings to file

    the problem is that im writing to file many times some thing like 5000+ times ( the reason is it writing to file directories and files )

    the code open file and writing to it looks like this :

    Code:
    ofstream myfile ("my_log.txt",ios::app);
    
    if (myfile.is_open())
    
    {
    
    myfile << c <<".\n";
    
    myfile.close();
    
    }


    how can i improve the speed of writing to file ?

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Don't open and close it so much. Open it once and leave it open until you are done.
    OracleGuy

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can i close the file only when the application exit ?

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Yeah or when you've done your 5,000 writes or whatever. Just make sure you do close it at some point.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •