0

I am trying to run the following program:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;
    double first=1.49, second=2.59, third=3.69, fourth=4.79;
    inFile.open("prices.txt");
    char response;
    if(!inFile.fail())
    {
        cout << "A file by the name prices.txt exists.\n" << "Do you want to continue and overwrite it\n" << " with the new data (y or n);"; cin >> response;
        if(tolower(response) == 'n')
        {
            cout << "The existing file will not be overwritten." << endl;
            return -1;

        }
    }
    outFile.open("prices.txt");
    if (inFile.fail())
    {
        cout << "\n The file does not exist and can not be opened" << endl;
        cout << "The file has been successfully opened for output." << endl;
        outFile << first << "\n" << second << "\n" << fourth << "\n" << endl;
        outFile.close();
        exit(1);
        cout << "The file has been successfully opened for output. " << endl;
        outFile << first << "\n" << second << "\n" << third << "\n" << fourth << endl;
        outFile.close();
        return 0;
    }
}

Yet this program will not write the values to the prices.txt file. If you run the program once it says the file does not exist. Running it a second time says the file is already there and if you want to overwrite it. The thing is searching my Mac I cannot find this file anywhere.

Any ideas what I am doing wrong with running it in Xcode? A friend runs the exact same code in Visual Studio 2008 and it works. Any help is appreciated.

3
  • Do you have sufficient permissions to write the file? Commented Jan 23, 2012 at 22:19
  • Xcode isn't an ideal IDE for just C++ development. Hybrid C++/Objective-C is fair enough but if you're writing a straight C++ program there are likely better cross-platform IDEs that you can use (such as Code::Blocks or NetBeans). This would also help with troubleshooting cross-platform development issues such as the one you're having, if everyone used the same IDE! Commented Jan 23, 2012 at 22:28
  • re outFile.open("prices.txt"); if (inFile.fail()) // shouldn;t this test be on outFile Commented Jan 24, 2012 at 16:05

2 Answers 2

2

You need to set the working directory for the executable since you are assuming that your data files are in the current working directory. In Xcode 3.x you set this in the first tab of Get Info for the executable. In Xcode 4.x it has been moved, but the principle is the same.

Alternatively you can change your data file paths (e.g. make them absolute) so that you do not make assumptions about the current working directory.

Sign up to request clarification or add additional context in comments.

Comments

0

You may not have permission to write into the directory that you are trying to save the file too.

Also, there is an error in your program and I am sure if it is there for debugging reasons. You have

    outFile.close();
    exit(1);

But then shortly there after you try to write to the file, then close it again.

2 Comments

yes it is for debugging purposes. How would i get correct permissions in Xcode so this would work?
It may be that you need to set the working directory, as explained in Paul R's post. I suggest taking a look at that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.