Giving a name inside the program to our text file using OFSTREAM ?

So, this way, when I execute the program the file has to be example.txt
But what I want to see if it's possible to be done is write the name of that file inside the program( the console).
Is this possible?


1
2
3
4
5
6
7
8
9
10
11
  #include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
Yes.

You surely studied basic I/O before moving on to fstreams?
http://www.cplusplus.com/doc/tutorial/basic_io/


Option 2. Command line arguments. The main() has two standard signatures:
1
2
3
int main()

int main( int argc, char * argv[] )

A program that uses the latter can handle strings that the user passes as part of program invokation.
Last edited on
Sorry, I somehow still didn't manage to find the answer can you please give me more information or an example?
Do you know how to read a string from the user?
Do you know how to read a string from the user?


I'm not sure what you mean by this, English is not my main language. Please show me an example of that and I'll let you know if I know it.
Last edited on
Well, one of the first thing people learn is often to use std::cin. Do you know how to use it, and do you know how to read a string (i.e. a piece of text) from the user. If you don't know this stuff I can only recommend you to read about it. It's probably at the start of every C++ book for beginners, or you could read about it in the tutorials on this site: http://www.cplusplus.com/doc/tutorial/basic_io/ (same link as given by keskiverto)
I'm not sure you guys understand what I'm trying to do

Here's a code I wrote:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct student {
        string name;
        string surname;

};

int main()
{
    string name, surname;
    int n;
    ofstream write;
    write.open("information.txt");
    student info[10];
    cout<<"Enter the amount of students" <<endl;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cout<<"Enter the "<<i+1<<" student name" <<endl;
        cin>>info[i].name;
        write<<info[i].name<<" ";
        cout<<"Enter the "<<i+1<<" student surname" <<endl;
        cin>>info[i].surname;
        write<<info[i].surname<<endl;
    }
    write.close();
    return 0;
}


As you can see this will create a file called information.txt
What I'm asking you, is it possible to make it inside the program to name that file?
Like:

What would you call this file to be called?
And I type in: information2
and the file saves as information2.txt

You can just read it the same way you read the name and surname and then pass the variable to the open function.
I'm trying that way but I keep getting an error, I'm not doing something right, can you help me out and change it in the above example so I can see where I'm doing the mistake?

Thank you!
My guess is you are getting an error because you are using something before C++11 and you can't give fstream a string. Otherwise you already seem to understand how to do it all?

1
2
3
4
string Filename;
cin >> Filename;
ofstream write;
write.open(Filename.c_str());

http://www.cplusplus.com/reference/string/string/c_str/
Thank you very much!
I was doing it without the .c_str() part
Topic archived. No new replies allowed.