C trouble

May 23, 2013 at 5:27am
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 May 23, 2013 at 5:28am
May 23, 2013 at 5:32am
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.
May 23, 2013 at 6:48am
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 May 23, 2013 at 6:49am
May 23, 2013 at 7:31am
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 May 23, 2013 at 8:04am
May 23, 2013 at 7:44am
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
May 23, 2013 at 9:57am
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.
May 23, 2013 at 10:19am
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
May 23, 2013 at 11:00am
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?
May 23, 2013 at 11:05am
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")
May 23, 2013 at 11:09am
Read about function parameters:
http://www.cplusplus.com/doc/tutorial/functions/
May 23, 2013 at 11:21am
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 May 23, 2013 at 11:40am
May 23, 2013 at 12:40pm
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 May 23, 2013 at 12:41pm
May 23, 2013 at 1:17pm
Ahhh thanks so maybe visual studio 2010 ? will support it ?
May 23, 2013 at 1:21pm
visual studio 2010
C++11
May 23, 2013 at 1:37pm
ok ok so it was a fail :D im downloading the 2012 one hope it solves my problem
Last edited on May 23, 2013 at 1:43pm
May 23, 2013 at 2:59pm
Guys thanks to everyone who helped me i ve done it
Topic archived. No new replies allowed.