Thread: ifstream function

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    ifstream function

    The test file for this function is located on my desktop in a folder.

    Is the syntax correct when (string folder) is being set to the test file at the file location?


    Code:
    ifstream get_ifs( )                             // get input file stream
    {
        string filename;                            // input file name
    
        cerr << "name of file to read from? ";
        cin  >> filename;
        string folder = "C:\\Users\CASHOUT\Desktop\week8\folder\test1.txt";                        // path for folder containing
                                                    // the .txt test files
        filename = folder + filename;
    
        ifstream ifs( filename, ifstream::in );
        if( ! ifs )                                 // cannot open input file
        {
            cerr << "cannot open input file '" << filename << "'\n";
            exit( 1 );
        }
    
        return ifs;                                 // return input file stream
    }
    Last edited by CASHOUT; 07-13-2013 at 12:10 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why line 5 uses error stream for regular output?
    What if file name contains spaces?
    Why folder name contains file name?
    Why utility function forces whole application to exit on error instead of throwing exception?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > string folder = "C:\\Users\CASHOUT\Desktop\week8\folder\test1.txt" ; // path for folder containing
    How many \ do you need ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remember that \ is an escape character, so whatever is inserted in the string is determined after the next character after the \.
    Additionally, you can use forward slashes (/) instead of backslashes in filenames. They work just as well.
    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.

  5. #5
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Salem View Post
    > string folder = "C:\\Users\CASHOUT\Desktop\week8\folder\test1.txt" ; // path for folder containing
    How many \ do you need ?
    after removing the extra \ :

    Code:
    string folder = "C:\Users\CASHOUT\Desktop\week8\folder\test1.txt";
    Now I'm being told:
    Error: incorrectly formed universal character name

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Like you were told, \ is an escape character. Therefore, you have characters \U, \C, \D, \w, \f, \t in there which I doubt you want!
    To product a \, you need to use \\.
    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.

  7. #7
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Elysia View Post
    Like you were told, \ is an escape character. Therefore, you have characters \U, \C, \D, \w, \f, \t in there which I doubt you want!
    To product a \, you need to use \\.
    I get what you mean now. Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-23-2010, 07:10 AM
  2. how to pass an ifstream object to a function
    By chintugavali in forum C++ Programming
    Replies: 14
    Last Post: 12-18-2007, 09:58 PM
  3. Passing a ifstream to a function
    By mike_g in forum C++ Programming
    Replies: 9
    Last Post: 10-01-2007, 04:38 PM
  4. Function/ifstream woes
    By Wraithan in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2005, 04:05 PM
  5. ifstream in a function
    By realjag in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2005, 04:31 PM