Ofstream parameter problem.

This is a discussion on Ofstream parameter problem. within the C++ Programming forums, part of the General Programming Boards category; Hi there, I am trying to allow a user to choose the filename for a file which will hold data. ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Ofstream parameter problem.

    Hi there,

    I am trying to allow a user to choose the filename for a file which will hold data.

    The data will be entered into the file with ofstream.


    I started with this in my program:

    Code:
    char fileName[20];        //NO BOUNDS CHECKING!!!!
    cout << "Enter the name of the .csv file" << endl;
    cin >> fileName;
    ofstream streamname(fileName);
    Since there is no bounds checking, I tried to use a string:

    Code:
    string fileName;
    cout << "Enter the name of the .csv file" << endl;
    getline (cin, fileName);
    ofstream streamname(fileName);
    But (unless I was mistaken?) it seems ofstream cannot accept a string as a parameter.

    So I tried this:

    Code:
    string tempfilename;
    vector<string>fileName;
    cout << "Enter the name of the .csv file" << endl;
    getline (cin, tempfilename);
    fileName.push_back(tempfilename);
    ofstream streamname(fileName);
    But then Visual Studio reports:
    cannot convert parameter 1 from 'std::vector<_Ty>' to 'const char *'
    Could anyone advise me on how do this is as simply as possible?

    Thanks for any help!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    string fileName;
    cout << "Enter the name of the .csv file" << endl;
    getline (cin, fileName);
    ofstream streamname(fileName.c_str());
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Hey thanks bithub!

    reading up on this, it converts the string to a c-style string, hence the error of "to 'const char *' "

    Great to learn this.

    Thanks buddy

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    IIRC, this issue should be fixed in the near future.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Replies: 10
    Last Post: 09-30-2009, 10:10 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 12:10 PM
  5. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21