// This program writes data to a file. // Converting string to c_str. #include #include #include using namespace std; int main() { ofstream outfile; // instantiate file pointer object string filename; // used to store filename to save file // get filename from user cout << "Enter the filename to store the file in\n" << "> "; cin >> filename; outfile.open(filename); // associate file with file pointer object //outfile.open(filename.c_str()); // convert filename to C-string in pre-C++11 if (!outfile) { cout << "Error opening file." << endl; return 1; // exit program with an error code } cout << "Writing to the file ... "; outfile << "KCMP\t" << 89.3 << endl; outfile << "KNOW\t" << 91.1 << endl; outfile << "KSJN\t" << 99.5 << endl; outfile.close(); // close the file! cout << "Done!" << endl; return 0; }