0
#Include<fstream>
#include<>
#include<iostream>
#include<string>
using namespace std;
int main()
{

    string str="time is a great teacher, but unfortunatly"
"it kills all its  puplis. Berlioz";
ofstream outfile("ali.TXT");//ERROR
for(int j=0;j<str.size();j++)
outfile.put(str[j]);
cout<<"file written\n";
//system("pause");
return 0;
    }

please correct my code"ofstream outfile("ali.txt")"

2
Contributors
1
Reply
8
Views
3 Years
Discussion Span
Last Post by nullptr
0
#Include<fstream> // should be #include (all lowercase)
#include<>        // what's this meant to be?

You should also check whether your outfile is open.

if (!outfile.is_open() )
{
    // print an error message if you like
    return 1;
}

After writing to outfile, close it: outfile.close();
Instead of writing the entire string character by character you could use:
outfile << str;

Also search these forums for tutorials on code formatting. It's far easier for both you and others to find errors when your code is well formatted.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.