Asks for a filename from the user using Character Array

I have a program that ask a user to give a name of a type of file such as .txt file, and then make 3 inputs and have those inputs save into this save file.

How do I have the user to make 3 inputs and save it to the text file named: myFile

ex: myFile.txt

-------------------------------------------------------------------

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main ()
{
char filename[16]; //this will allow the file name be 16 in length
cout << "Enter the name of an existing file: ";
cin.getline (filename,16);

cout << "\nThe name of your file is: " << filename << endl;

ofstream myFile; // this will allow for file to be saved
myFile.open (filename); // open file named: myFile

if( myFile.good() )
cout << "\nFile open is OK \n";
else
cout << "\nfile open FAILED \n";



myFile.close(); // close file after anything it saves to it

cout << "\n\n";
system ( "pause" );
return 0;
}
Last edited on
what kind of input do you want to insert to your .txt file?

edit:
i'm a beginner too.. but i have given this thing before..
so let me help you.. :)

first, you have to know what kind of data you want to insert into the text file..
for instance, we want to create a data file consist of people's name based on user input..
then we can use a character array..

so just define it char userInput [size];
the size is depends on you.. let's say the size is 20..

let user input the data..

1
2
3
cout << "Please enter the data that you want to insert into the text file: ";
cin >> userInput;
 


because i don't know how many data you wish to insert, i guess it's better to use do-while..
something like this..

1
2
3
4
5
6
7
8
char option;

do
{
     //the code above
     cout << "Wish to enter another data (Y/N)?" ;
     cin >> option;
} (option != n);


then.. you can write those data into the text file, just like a normal cout..
but the difference is that you use the name that you declare using fstream..
something like this :
myFile << userInput << endl;

i hope this can help.. :)
Last edited on
You can use just a std::string for all your user interaction. It is better and has no baffling, artificial limits.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
  string filename;
  cout << "Enter the name of an existing file: " << flush;
  getline( cin, filename );

  cout << "\nThe name of your file is: " << filename << endl;

  ofstream myFile( filename.c_str() );  // The c_str() member function gives you a c-style string

  if (myFile.good())
    cout << "\nFile open is OK\n";
  else
    cout << "\nFile open FAILED\n";

  // Now, you can use another string the same way you did above to get user inputs to save to file
  string s;
  cout << "\nPlease input something: " << flush;
  getline( cin, s );
  // (If you must do any additional processing, such as checking for an integer, etc,
  //  use a std::stringstream and do it here. When you are done, output
  //  whatever you want to the file.)
  myFile << s << endl;

  // Do that three times. I suggest a loop or something.

  myFile.close();

  cout << "\n\nPress ENTER to quit..." << flush;
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  return 0;
}

Hope this helps.
Thanks Duoas...it worked perfectly...
Topic archived. No new replies allowed.