Thread: Very Easy Question

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Very Easy Question

    Code:
    ifstream file_input("myinputfile.txt");
    
    cout << file_input.something; // prints "myinputfile.txt"
    What is the "something" above?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Like this?
    Code:
    #include <iostream>
    #include <string>
    
    struct ifstream
    {
        std::string something;
        ifstream(const std::string& sth): something(sth) {}
    };
    
    using std::cout;
    
    int main(){
        ifstream file_input("myinputfile.txt");
    
        cout << file_input.something; // prints "myinputfile.txt"
    }
    Or what is your question? I don't think ifstream stores the name of the file if that's what you mean. If you opened the file, the name was available to you - why can't you use that?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by anon View Post
    Like this?
    Code:
    #include <iostream>
    #include <string>
    
    struct ifstream
    {
        std::string something;
        ifstream(const std::string& sth): something(sth) {}
    };
    
    using std::cout;
    
    int main(){
        ifstream file_input("myinputfile.txt");
    
        cout << file_input.something; // prints "myinputfile.txt"
    }
    Or what is your question? I don't think ifstream stores the name of the file if that's what you mean. If you opened the file, the name was available to you - why can't you use that?
    I want to have an error when the file can't be opened. I want it to say:

    cout << "Error! " << (filename) << " could not be opened!";

    The name of the file will eventually be a command line argument to the program, therefore it will change.

    Not possible?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If it is a command line argument then it is stored in a variable? Why can't you use that?

    Code:
    string name = "somefile.txt";
    ifstream fin(name.c_str());
    if (!fin) {
        cout << "Couldn't open " << name;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're specifying the filename when you open the file, so you must know it.
    If it can't be opened, just print that filename.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by Elysia View Post
    You're specifying the filename when you open the file, so you must know it.
    If it can't be opened, just print that filename.
    Ah yes... Nevermind

    When I (eventually) code it to work with command line arguments, I can access it that way through those arguments

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM