Thread: having a problem using variables in file calls

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    32

    Unhappy having a problem using variables in file calls

    I've seen several postings in regards to this and tried about 40 times to correct the problem, using different methods. My code needs to open a remote file not in the same directory so when I put the variable name in my file path it is not finding my file.

    {
    cout << "Please Enter the Date you would like to search for: \n"
    << "(NOTE: MUST BE IN MONTH DAY YEAR FORMAT )";
    cin >> fileName;
    ifstream fin;
    fin.open ("C:\\fileName", ios::in | ios::nocreate);
    if (fin.fail())
    {
    cout <<"ERROR: Unable to open the snap server file"<<endl;

    }
    I've tried using fin.open("C:\\(fileName.c_str())) and still can't find my file, which I know is there. if enter the string name not using the variable it finds the file just fine, and I if put the file in the same directory that the compiler is using the I can use the variable name "fileName" it finds the file fine. ???? What am I doing wrong when I use a path name and the string varialbe together? Please help.....
    I'd like to put Murphy and his laws in headlock sometimes!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >"C:\\fileName"
    This tries to open the file called fileName.

    >"C:\\(fileName.c_str())
    This doesn't close the string literal, which is a syntax error.

    Keep in mind that C++ doesn't support variable interpolation, you must build the string before passing it to open:
    Code:
    string full_path = "C:\\";
    .
    .
    .
    full_path += fileName;
    fin.open ( full_path.c_str(), ios::in | ios::nocreate );
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    32
    ah, ok, so in order to use the function .c_str() in the manner I am trying to use it, I declare another constant string that is the path, leave my string for the user to input and use the += to combine the two strings and use that with the .c_str() function.

    that makes alot of sense, I will try that and manipulate that as best as possible, thanks for the example and description on how it works!
    I'd like to put Murphy and his laws in headlock sometimes!

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. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 08:43 PM
  4. Replies: 6
    Last Post: 03-06-2005, 02:43 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM