C trouble

So hi guys im new here. I'm trying to learn c++

1
2
3
4
5
6
7
  char path[80];
  cout<<"Enter the path:";
  cin>>path;
   
  ofstream myfile;
  myfile.open ("");
  


I would like to know how can i put this char"path" into the brackets in myfile.open.I mean if i cin test.txt how can i put it into brackets. I think it is going to be easier ,than i expect but i can't get it.Thx guys for help
Last edited on
myfile.open(path.c_str());

If i recall, I had this similar problem and this is what I found to get it to work. This worked on a file name, not sure if it will work for a path. Try this at least.
c_str() works with the string class to convert a string to a c_string so it won't work here.
try this
myfile.open (path)
Last edited on
Neither char array nor open are to be recommended.
1
2
3
4
5
6
7
8
9
10
std::string path;
std::cin >> path;
if ( 0 < path.size() )
  {
    std::ofstream myfile( path );
    if ( myfile )
      {
        // write to myfile
      }
  }
Last edited on
std::cin << path; You probably intend to use >> here.
Actually opening file in constructor is just neat looking thing. It does not give any actual benefits (aside from impossibility to use uninitializated stream). Constructor with parameters differs from default one only by call to file.open() at the end on most implementations
Fixed.

Part of looking neat is the implication that myfile follows the RAII idiom. Using well-known idioms and patterns can make the code easier to maintain.
I couldnt solve my problem.I dont know what to do.Isnt there easier way to get the content of path to the brackets in opening ? But thanks for the replies
Post number 4 is an easy method. get the user input and put it in a variable.
Not sure how you could make that easier?
std::ofstream myfile( path );
if ( myfile )
{
// write to myfile
}


this stage is not clear for me especially this (path) in brackets is wrong because compiler says it has to be ("path")
Read about function parameters:
http://www.cplusplus.com/doc/tutorial/functions/
i know but it does not allow me compile while there is no ("path"). and then it makes only a file named path all the time .I hope we are talking about same thing.


My idea is: for example i write in program name of txt file results.txt it makes a file and save results there manualy if i replace ("path") to ("resultst.txt") it works.
Last edited on
DeniLeet wrote:
this stage is not clear for me especially this (path) in brackets is wrong because compiler says it has to be ("path")

Your compiler does not support C++11. In C++11 there are two versions of ofstream constructor. First takes const char * as parameter and the second takes const std::string &. Previous C++ had only the first form.

std::string path is not of type const char *. Earliers posters did mention c_str(). That member function of std::string does return a const char *. Therefore:

std::ofstream myfile( path.c_str() );
Last edited on
Ahhh thanks so maybe visual studio 2010 ? will support it ?
visual studio 2010
C++11
ok ok so it was a fail :D im downloading the 2012 one hope it solves my problem
Last edited on
Guys thanks to everyone who helped me i ve done it
Topic archived. No new replies allowed.