How do i write a for loop to a file?

Hi.
I'm just wondering how i write a for loop to a file. I've tried to look at generals examples on how to write to a file, but i never managed to get this to work.
What i need for now is to write this result to a text file:

int i;
for (i=32; i<126; i++)
{
cout << (char) i << " " << (int) i << endl;
}

Thanks!
1
2
3
4
5
6
7
8
9
ofstream fout("filename.txt");
if (!fout) {
    cerr << "Could not open file." << endl;
    return 1;
}
for (int i = 32; i < 126; ++i) {
    fout << (char) i << " " << i << endl; // don't need to cast int to int
}
fout.close();
Awesome :D
Topic archived. No new replies allowed.