samedi 24 juillet 2021

This code allows the user to specify the file name to be created. When the file is created, it does not have a .txt extension. How do i fix this?

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
  {

//Declaring file.

  ofstream f;
  string filename;

  cout << "Please enter a file name to write: ";
  getline( cin, filename );

//If you can also explain what .c_str() does here that would be awesome.

  f.open( filename.c_str() );
  f << "Hello world!\n";
  f.close();

  cout << "Good job! 'type' or 'cat' your new file to see what it says.\n";
  return 0;
  }

Aucun commentaire:

Enregistrer un commentaire