i was wondering if you can have the file name the file ofstream makes when you run your program a string. like if the user wanted to have there own name for the file. thx
i was wondering if you can have the file name the file ofstream makes when you run your program a string. like if the user wanted to have there own name for the file. thx
You mean like this:
Code:std::string FileName; std::ofstream File; ... std::cout << "Enter a filename: "; std::cin >> FileName; ... File.open(FileName.c_str(), std::ios::out); if(!File.fail()) { ... File.close(); }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
ummm....maybe, ur code is extremely hard to read. i just want to know if u can have a string be a filename
>i just want to know if u can have a string be a filename
Yes.
My best code is written with the delete key.
so would it be like this:?
[code]
char name[50]
cout << "Please enter the filename title: ";
cin >> name;
ofstream a_file(name)
[code]
? i didnt include everything needed, like text to be put in the file. but is that how it would look?
>but is that how it would look?
More or less. Have you tried it yet to see if it works? Writing little test programs to see if your assumptions are correct is a useful habit to get into.
My best code is written with the delete key.
isn't the filename by default a string? You mean have the user rename the file correct?Originally posted by oobootsy1
ummm....maybe, ur code is extremely hard to read. i just want to know if u can have a string be a filename
Warning: Opinions subject to change without notice
The C Library Reference Guide
Understand the fundamentals
Then have some more fun
....i dont have the internet where my computer is, so i only get on the internett at library and such. i mean have the filename a string!
Yes, your example is what it would look like, except you forgot the semicolon after "char name[50]" and after "ofstream a_file(name)". That's one way to do it. Although Magos' example is more fool-proof.
Code:string name; cout << "Please enter the filename title: "; cin >> name; ofstream a_file(name.c_str());
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.