Opening File with function

This is a discussion on Opening File with function within the C++ Programming forums, part of the General Programming Boards category; Ok so I have to open a file using a function with error checking.. But I can't get the file ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    68

    Opening File with function

    Ok so I have to open a file using a function with error checking.. But I can't get the file to open..

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        ifstream myIn;
        
        OpenFile(myIn);    
    
            
        myIn.close(); 
        
    }
    
    void OpenFile(ifstream& myIn)
    { 
        string filename;
        do{
        cout << " Please enter the file name to receive student information from: ";
        cin >> filename;
        
        myIn.open(filename.c_str());
        }while (!myIn);
         
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by bleuz View Post
    Ok so I have to open a file using a function with error checking.. But I can't get the file to open..
    So, what does your error checking tell you????? LOL Sorry, couldn't resist. Now I'll read your code...
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Your code seems "ok". Try entering a full path to the file and see if that works. If it does, then your current working directory is working against you.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    haha Dino. Its all good man. I was waiting for that one from someone

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    And, you obviously need more code in your do / while logic than what you have.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    C:\Users\Bleuz\Desktop\student_info.txt


    That did not work... Well idk man... question. How would you go about doing what I am trying.

    and would a UNIX enviroment handle this differently perhaps?

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Other than OpenFile being misplaced, it works for me.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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