TEXT FILE

I'm sending data to text file 2500 numbers are created and each of them are being sent individually but every single time it says that text file is being modified outside the source editor and it asks if I want to change it so I have push the button 'yes ' for 2500 times.

Is there anyway to prevent the program to ask every single time
Cache all the numbers together and write them out in one go?
what do you mean cache how am I gonna do that?
Instead of writing 1 character 2500 times, write 2500 characters once.
but ı have a function that generates one number each time and the generated number is transferred to text file then function generates another one ....
Its a for loop
so can you be a little more precise about what I should do please?
Well. one method would be to use a stringstream.
http://www.cplusplus.com/reference/iostream/stringstream/

This is something I've not used much, so my advice may not be the best here, but here's an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

    using namespace std;

int main()
{
    ostringstream outs;          // create a stringstream

    for (int i=1; i<=2500; ++i)
        outs << i << endl;       // output number to stringstream

    ofstream outf("output.txt"); // Open output file
    outf << outs.str();          // output the string to the file
    
    outs.str("");                // Empty the stringstream 
  
    return 0;
}
Topic archived. No new replies allowed.