Open File

This is a discussion on Open File within the C++ Programming forums, part of the General Programming Boards category; Hey, I am making a program where the user is asked what filename he wants to open. This is what ...

  1. #1
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47

    Cool Open File

    Hey,
    I am making a program where the user is asked what filename he wants to open.

    This is what I have:

    cout<<"Enter File Name"<<endl;
    cin>>filename;

    ifstream inl(filename.txt);

    What do I need?
    Thanks

  2. #2
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    i could be wrong but i believe the ifstream constructor takes a c-string (char*). so you would have to have

    Code:
    cout<<"Enter File Name"<<endl; 
    cin>>filename; 
    
    ifstream inl(filename.c_str());
    string::c_str() returns its c-string counterpart.

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    you are right... he can also do this

    ...

    char str[30];

    cout << "Filename";
    cin >> str;

    ifstream ins;

    ins.open(str);

    ...


    >>>ifstream inl(filename.txt);

    oh... and the .txt part doesn't work like that. That wouldn't compile.
    Blue

  4. #4
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    it would work if "txt" was a variable inside the structure "filename" but it isn't, so ya, it wouldn't compile.

    When I first glanced I thought all you was trying to do was open a file named filename.txt.

  5. #5
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47
    Hey Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

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