Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

infydave

Inserting keyboard defined variable into an fstream parameter

This topic is 5331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is this possible? For instance - I ask someone to enter their name. That''s fine. Then I take that name which they entered, and insert it into an fstream parameter as shown - (this is just a snippet, not the whole thing) char username[15]; cout << "\nEnter your name - "; cin >> username; fstream c_file("<< username <<.txt", ios_base::in); if (!c_file) { //no such file c_file.close (); cout << "\nFILE STATUS : Record file created successfully.\n"; recordfile.open("<< username <<.txt"); I haven''t only tried the ''<<'' to insert the username variable into the parameter, it''s just the last thing I tried. Is there any hope? Surely it''s simple enough, I just can''t find the answer Also another problem I have is a cin.getline above that code asking for the user''s name. The problem is that the previous function which goes to that one, has to have enter pressed, (after selecting an option). This seems to bypass the cin.getline. I wouldn''t have thought that should happen but I assume it''s because it''s picking up the ''\n'' character when I press enter. Any ideas about that either? Any help is appreciated, Dave

Share this post


Link to post
Share on other sites
Advertisement
The insertion operator, << is defined for output stream objects. cin is not a function (despite what many crap textbooks say), it is an object - an instance of std::basic_ostream<char>. The insertion operator is not generally defined for text concatenation and so forth.

To concatenate text, you can use a std::string or a std::stringstream. Use the latter if you''re concatenating text, numbers and other values with defined insertion operators (eg, you could write a Complex number class and provide the insertion operator for it).

So your example should look like this:

#include <iostream>
#include <string>

int main()
{
using namespace std;

...

string username;
cout << "\nEnter your name: ";
getline(cin, username); // using getline allows for spaces in the username

string filename = username + ".txt";

ifstream c_file( filename.c_str() ); // since you only want input, use an ifstream

if( !c_file )
{
// no such file

//*** note that there is no need to close c_file since it was never opened

cerr << "File \''" << filename << "\'' not found." << endl;
return -1;
}

cout << "FILE STATUS: Record file created successfully." << endl;

...

return 0;
}


For your extra newline problem with getline, take a look at this page.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!