Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble with fstream in Xcode
    primarykey
    data
    text
    <p>I'm having trouble validating the existence of REGISTER.txt for input purposes in a function (see below). My understanding is that if the file doesn't exist, then the file won't be opened and the file stream variable (inData) will be false. Thus, I can use that variable in an if/else statement to verify whether or not it opened. But even though REGISTER.txt is in the same directory as my .cpp file, my code still says that it wasn't opened.</p> <p>Here's the thing though. When I run the same exact code in Dev-C++ compiler, it works fine and the file is found. Now, I understand compilers are different, but I don't understand what is causing the discrepancy here. My preferred IDE is Xcode, so I'd like to learn how to do I/O with files in Xcode.</p> <p>Thanks in advance for the help.</p> <p>P.S. My Xcode project references the file, so it's not like the project isn't connected with the file.</p> <pre><code>void ReadVehicleRegInfo(char&amp; vehicleType, string&amp; licensePlate, int&amp; modelYear, float&amp; origTaxValue, bool&amp; error) { ifstream inData; string inputFile = "REGISTER.txt"; inData.open(inputFile.c_str()); //File contains registration info if (!inData) { //File does not exist. Exit function cout &lt;&lt; inputFile &lt;&lt; " does not exist. Program will now terminate" &lt;&lt; endl &lt;&lt; endl; error = true; return; } else { //File exists - continue with program cout &lt;&lt; inputFile &lt;&lt; " found"; } inData.close(); } </code></pre> <p>In my main() function, I have the following code to signal to the user that an error has occurred:</p> <pre><code>if (error) { //Function encountered error. Exits program system("PAUSE"); return 99; } </code></pre> <p><em><strong>EDIT</em></strong> I spent 40 minutes trying to figure this out, 15 writing the question, and 5 minutes after I post it I make huge progress. Don't you love that?</p> <p>I put in the full directory to the file and that did the trick. </p> <p>However, this is not ideal. The next question is how do I avoid having to do that? What is the default directory for Xcode?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload