0

sorry if this is an amateur question but I am new to coding and I am trying to open/read a file through a function?

My code for the function is kind of like this:

struct_name ReadInFile(std::string fileName)
{
    std::ifstream readFile("fileName");
    assert(readFile.is_open());
...
...
}

but it fails at this assert stage? (I know assert isn't good coding practice!)

The main programme looks like this:

struct_name TESTING = ReadInFile("thefile.dat");
std::cout << TESTING.struct_variable;

I am not sure why the file won't open - I assume it's because it's not saved in the right place on my computer (I think it needs to be stored wherever my executable file gets built right?) but I have tried saving it in 50+ different locations and still nothing works!?

/Users/MyName/Library/Developer/Xcode/DerivedData

is where I'm told my build system is but I have saved it in numerous locations around here and still nothing!

I would really appreciate some help! Thank you so much

drescherjm
  • 10,365
  • 5
  • 44
  • 64
learning
  • 11
  • 2
  • The working directory is the place to put your text file unless you use a full path. Related: [https://stackoverflow.com/questions/3396378/change-the-working-directory-in-xcode](https://stackoverflow.com/questions/3396378/change-the-working-directory-in-xcode) – drescherjm Nov 23 '21 at 15:04
  • Usually Xcode builds app bundles and the working directory of the running app is inside the bundle. The bundle does not have fileName. – 273K Nov 23 '21 at 15:10
  • I'm still not really sure how to change my settings/code to help me...:( – learning Nov 23 '21 at 23:06

0 Answers0