is it possible to convet a *char to a string

Discussion in 'OT Technology' started by Stemby, Nov 28, 2005.

  1. Stemby

    Stemby Whoooose House? COOOOOOOOOOOOGS HOUSE!

    Joined:
    Sep 16, 2004
    Messages:
    3,465
    Likes Received:
    0
    Location:
    Houston,Tx
    char *getname=new char[100];
    cout<< "Enter file name to be opened: " ;
    cin>> getname;
    static cast<string> getname; /error on this line so how do i fix it

    i need to get a file name manually inputted to be opened just in BASIC C++
     
  2. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    If you are using the string class you can do something like this:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	string fileName;
    	ifstream inFile;
    
    	cout << "Enter file name: ";
    	cin >> fileName;
    
    	inFile.open (fileName.c_str(), ifstream::in);  // notice use of c_str()
    
    	// do stuff with file
    
    	inFile.close();
    
    	return 0;
    }
    
    
    If you are using a character array you can do something like:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	char *fileName = new char[100];
    	ifstream inFile;
    
    	cout << "Enter file name: ";
    	cin.getline (fileName,99);
    
    	inFile.open (fileName, ifstream::in);
    
    	// do stuff with file
    
    	inFile.close();
    	delete[] fileName;
    	
    	return 0;
    }
    
    http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
     
  3. Stemby

    Stemby Whoooose House? COOOOOOOOOOOOGS HOUSE!

    Joined:
    Sep 16, 2004
    Messages:
    3,465
    Likes Received:
    0
    Location:
    Houston,Tx
    thanks a bunch
     

Share This Page