Reading from a file inputted by user

This is a discussion on Reading from a file inputted by user within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get file i/o working for my program. I've gotten it to read from a file when I ...

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Reading from a file inputted by user

    I'm trying to get file i/o working for my program. I've gotten it to read from a file when I go and do:
    Code:
    ifstream fin("Info3.txt");
    fin.getline(cipher, *sizeOfMessage, '\n');
    The program reads the file and there are no problems. The thing I want to do is ask the user for the filename and insert the filename where Info3.txt is. The problem with doing that from what I've figured out is that the program believes that the variable is the filename. The only thing I could think of that would fix this is to go like this:
    Code:
    ifstream fin(userInput);
    fin.getline(cipher, *sizeOfMessage, '\n');
    Where I have the variable userInput which contains the filename. But I would have to write code that would insert quotation marks at the beginning and end of the variable userInput

    Does anyone have any ideas to fix this other than the way I came up with?

    Thanks,

    ...Dan

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    erm I take it you hasve the input name as a string?

    I think you use ifstream fin( userinput.c_str() );

    you could also just get the name of the file then add ".txt" or otrher fileextensions to it.

    Edit:

    Code:
    userinput = userinput + ".txt"                                
    
    //if you want to add the file extension rather than the user input it
    
    ifstream fin ( userinput.c_str() );
    Last edited by bumfluff; 03-31-2006 at 10:03 AM.

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Alright that makes sense...I saw something like that on the net when I was searching. I'll give it a try.

    Thanks,

    ...Dan

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Sorry I jsut added the code abvove...if it works....I am a noob btw so I could be horribly wrong...it just worked in my programs

  5. #5
    System-7
    Join Date
    Nov 2005
    Posts
    65
    What you originally had worked. Thanks

    ...Dan

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    wow cool

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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