Like Tree1Likes
  • 1 Post By Salem

ifstream function

This is a discussion on ifstream function within the C++ Programming forums, part of the General Programming Boards category; The test file for this function is located on my desktop in a folder. Is the syntax correct when (string ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > string folder = "C:\\Users\CASHOUT\Desktop\week8\folder\test1.txt" ; // path for folder containing
    How many \ do you need ?
    iMalc likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    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, 08: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, 03:05 PM
  5. ifstream in a function
    By realjag in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2005, 04:31 PM

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