Thread: Opening File with function

  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
    Chappell Hill, Texas
    Posts
    2,332
    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...
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    And, you obviously need more code in your do / while logic than what you have.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    Other than OpenFile being misplaced, it works for me.
    Mainframe assembler programmer by trade. C coder when I can.

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, 01: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