Thread: file name as a string

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    file name as a string

    hey guys, is there a way to send a file name as a string? I get an error if I leave out the quotes around fileName, and the opening failes if they are there...any suggestions? assume that the file that user input exists in the directory ofcourse...

    Code:
    void generateText(string fileName, int window, int length, char* text)
    {
    	ifstream inStream;
    
    	inStream.open(fileName); // the problem is here...I assume
    	if ( inStream.fail() ) 
    	{
    		cout << "Input file opening for " << fileName << " failed.  Exiting...\n\n";
    		exit(-1);
    	}
    }
    thanks, axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What does
    Code:
    cout << "Input file opening for " << fileName << " failed.  Exiting...\n\n";
    output?

    gg

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    try
    Code:
    inStream.open(fileName.c_str());

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Codeplug:

    if fileName is enclosed in quotes like inStream.open("fileName"), the program outputs exactly what is written with the name of the file that is in fileName. if fileName doesn't have quotes, this is the error I get
    Code:
    C:\Program Files\Microsoft Visual Studio\MyProjects\prog4\b.cpp(143) : error C2664: 'void __thiscall 
    std::basic_ifstream<char,struct std::char_traits<char> >::open(const char *,int)' : cannot 
    convert parameter 1 from 'class std::basic_string<char,st
    ripper079:
    thanks, your suggestion works...I looked up c_str() and just for my knowledge, and correct me if I'm wrong, it converts the string to a c-string?, is it just in that instance, and the original variable is just a normal string, or is is a c-string from here on?

    thanks,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I keep forgetting string doesn't overload "const char*" operator...

    Calling c_str() will simply return a "const char*" version of fileName (in other words, a null-terminated C-string). It won't modify fileName.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM