Thread: Problem writing function that opens file by user-inputed file name??

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    Problem writing function that opens file by user-inputed file name??

    Hi,
    I'm having trouble in one part of my code. My code is supposed to write/use a single function that opens a single file (and checks for a successful open) but, you must ask the user for the file name, as opposed to using fixed names.

    I'm writing a void function called OpenCheck to do this but i am not sure on how to get the user-inputed filename to open in my function. Can anyone help me out?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    void OpenCheck(ifstream&, string);
    int ReadFirstFile(ifstream&,int[],string[]);
    
    int main()
    {
        ifstream inFile1, inFile2;
        int num, count;
        string name;
        string file1, file2;
        
        cout << "Enter a name for first input file:";
        cin >> file1;
        cout << "\n Enter a name for the second input file:";
        
        OpenCheck(inFile1, file1);
        OpenCheck(inFile2, file2);
        
        return 0;
        system("Pause");    
    }
    
    //functioin definition for OpenCheck
    void OpenCheck(ifstream&, string file)
    {
         inFile.open(file.c_str());
         
         if(inFile.fail())
         {
             cout << "Error Opening "
             << filename
             << ". Program Terminated. \n";
             system("Pause")
             exit(1);
         }
    }
    
    //function definition for ReadFirstFile
    int ReadFirstFile(ifstream&,int[],string[]);
    {
        count = 0;
        // priming read
        inFile1 >> num;
        getline(inFile1,name);
        while (!inFile1.eof())
        {
            // store data in the integer and string arrays
            integerArray[count] = num; // come up with better variable names
            stringArray[count] = name; // than integerArray & stringArray
            count++;
            // repeat priming read
            inFile1 >> num;
            getline(inFile1,name);
        }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What's the problem? The basic setup looks good.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    11
    When i type in pa8.in for my first input file,

    It says "Error opening pa8.in. Program Terminated"

    But my pa8.in is in there.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use a full path (e.g. C:/MyFiles/pa8.in) and see if it works. If it does, you have a problem with the working directory not being what you expected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM