Thread: Opening files, easy question but I can't find the answer anywhere

  1. #1
    Unregistered
    Guest

    Opening files, easy question but I can't find the answer anywhere

    Hi,

    I've got a problem with opening files to output to. I'm being passed a string, but I've got to use that to open up a file without using a dynamic character array. I can use stringstreams but I don't know how I can convert my string into something that an ofstream.open command will accept as an argument. Any help would be appreciated.

    -Adam

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    You could use open but I open the file in the ofstream constructor.

    Code:
    #include <iostream>
    #include <fstream.h>
    using std::string;
    
    int main() 
    {
    	string strFilename( "c:\\dang.txt" );	
    
    	fstream oFile( strFilename.data( ), ios::out, filebuf::sh_write );
    
    	if( oFile.is_open( ) )
    	{
    		oFile.close( );//could just let the ~fstream destructor close it
    	}
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    why can't you do something simple like declaring an array that should hold any possible input string to accept input then proceed as usual. You may have to add double backslashes if path has backslash(es) in it when written in standard english.

    char input[256];

    //filename for out put file coming from an input file

    ifstream fin("data.txt");
    fin >> input;

    //now use input as name of output file
    ofstream fou(input);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. files question
    By Micko in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2004, 02:12 PM
  2. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  3. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM
  4. Replies: 22
    Last Post: 11-08-2001, 11:01 PM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM