Thread: question about using a string as a file location

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    question about using a string as a file location

    hi i've never posted here before but ive got a problem and none of my friends could help me figure it out. basically i have a variable called fileloc, its a string entered by the user. my problem is how to i make this string the location where the file is written to. here is the part of my code in question
    Code:
    #include <iostream>
    #include "Rectangle.h"
    #include <string>
    #include <fstream>
    using namespace std;
    #include <iomanip> // given to me by the tutor in room 320
    
    int main()
    {
    	Rectangle r;
    	float l;
    	float w;
    	char answer;
    	string fileloc;
    	ofstream output;
    	
    	//program info
    	output<<fixed<<showpoint<<setprecision(1);
    	cout<<"This is the rectangle program designed to find the perimeter and area of "<<endl
    		<<"rectangles of custom length and width which is input by the user and"<<endl
    		<<"finally print the output to a user selected file"<<endl<<endl;
    	// ask the user where to save it
    	cout<<"Where would you like to save the output file"<<endl;
    	getline(cin,fileloc);
    	//open the file
    	output.open(fileloc, ios::out);
    and this is the error i get:

    ..\..\program 2\program 2\project 2.cpp(39) : error C2664: 'void std::basic_ofstream<_Elem,_Traits>:pen(const wchar_t *,std::ios_base:penmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    im not very good at c++ (still learning) so maybe i have made a stupid mistake but any help would be awesome thanks in advance

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    use:

    Code:
    output.open(fileloc.c_str(), ios::out);
    -- Rocky
    -- DreamSys Software (http://www.dreamsyssoft.com)
    -- Free Tiff 2 PDF Library (http://www.dreamsyssoft.com/tiff-to-pdf-api)

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    thank you that worked great i really aprciate it

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    The problem is iostream expects a constant char array.

    http://www.cppreference.com/cppstring/c_str.html

    explains the usage of c_str().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM