ifstream does not work in Xcode?

10,572

Solution 1

You have two options:

1) Use Objective-C++ ( remove the .m extension and replace it with .mm)

2) Go to the project settings and add those values:

GLIBCXX_DEBUG=1 
GLIBCXXDEBUGPEDANTIC=1

in the preprocessor macros.

Older Xcode: image ! Newer Xcode:

image!

Example code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, const char * argv[])
{
    ofstream myfile;
    myfile.open ("/Developer/afile.txt");
        if(!myfile)
        {
        std::cout << "ERROR" std::endl;
        }    
    myfile << "Writing this to a file.\n";
    myfile.close();
    return 0;
}

Solution 2

If you don't want to mess with anything in Xcode, fearing that you might break some other setting, do this:

As soon as you build and run your console program or app, go to the file list usually on the left side where the project is laid out, find the "Projects" directory, right click or option-click and select "Show in Finder". This is where Xcode dumps your builds every time you run it for that particular project. Every workspace will have its own build folder. You should find the file you selected or created there, if you want to add your own file there just drag it and you can then call it from your console or app program: myfile.open("file.txt");

Or just change the Working Directory for the project and use that folder which is probably a lot more accessible than /Developer/Gibberish/Build/Debug/whatever.

This makes it easy to port your code over to Win32 or other systems, at least for me it has.

Share:
10,572
BlueBear
Author by

BlueBear

iOS Software Engineer. SOreadytohelp

Updated on August 22, 2022

Comments

  • BlueBear
    BlueBear over 1 year

    I am attempting to work with some files using an ifstream. Everything appears to be fine, but when I attempt to open a file it will not work. Whether I attempt it as inputting a string variable or a string literal for the name. The files that I am attempting to access are in the same directory as the project and do contain contents.

    The project does not display any errors and will compile, but it will just say that it can't access the file every time.

    The additional headers, "simpio.h" and "console.h" are just libraries provided by stanford.

    #include <iostream>
    #include "console.h"
    #include "simpio.h"
    #include <fstream>
    #include <string>
    using namespace std;
    
    int countLines(ifstream & in)
    {
        int count = 0;
        while (true) {
            string line;
            getline(in, line);
            if (in.fail()) break;
            count++;
        }
        return count;
    }
    
    int main()
    {
        ifstream in;
        while (true) {
            cout << "Enter name: ";
            string s = getLine();
            in.open(s.c_str());
            if (!in.fail()) break;
            cout << "Couldn't open file, try again!" << endl;
            in.clear();
        }
    
    
    
        cout << "Num lines = " << countLines(in) << endl;
    
        return 0;
    }
    

    Any help would be great! Thanks!