Open file from user input?

This is a discussion on Open file from user input? within the C++ Programming forums, part of the General Programming Boards category; using fstream, i can't seem to open a file from user input for example i cin a string from the ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    5

    Open file from user input?

    using fstream, i can't seem to open a file from user input

    for example i cin a string from the user and try infile.open("A:" + string) but that doesn't work, can someone tell me how to do this? that's the last thing i need to finish my school project

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Post the code you have so far.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i cin a string from the user and try infile.open("A:" + string) but that doesn't work
    If your string is a C-style string then there's no way that will work. You need to do something like this:
    Code:
    char *filename = new char[strlen ( string ) + 3];
    strcpy ( filename, "A:" );
    strcat ( filename, string );
    infile.open ( filename );
    delete filename;
    If your string is a C++ string then things are much easier (if slightly more obscure):
    Code:
    infile.open ( ("A:" + string).c_str() );
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  5. open input file twice?
    By keithmolo in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2003, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21