File INPUT

This is a discussion on File INPUT within the C++ Programming forums, part of the General Programming Boards category; hey, I am working on a program in which i have to ask the user for a file name and ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    File INPUT

    hey,
    I am working on a program in which i have to ask the user for a file name and then check for errors and read data from that file. I'm not sure how to do this. if I cin the file name as a string how do i use it?

    any help is appreciated.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    
    ...
    
    std::string str;
    std::cout << "Enter filename: ";   // Prompt for filename
    std::cin >> str;                   // Get filename
    std::ifstream input(str.c_str());  // Open file
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    thanks alot for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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