file io with variable

This is a discussion on file io with variable within the C++ Programming forums, part of the General Programming Boards category; I want the user to enter then name of the file to open so i did this basicly cin>>name; /* ...

  1. #1
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    file io with variable

    I want the user to enter then name of the file to open so i did this basicly

    cin>>name; /* this isnt real code */
    ifstream ifile;
    ifile.open(name)
    blah

    but it doesnt work...how would I do this?
    +++
    ++
    + Sekti
    ++
    +++

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    char filename[80];
    cin.getline(filename,79,'\n');
    ifstream input;
    input.open(filename);
    if( input.fail() )
    {
    cout << "Error with file " << filename << endl;

    }
    else
    {
    //do whatever you want with the file
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 09:18 AM

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