1

For the last week, I've not been able to open files in Xcode. I'm looking to learn why it isn't working, and how to fix it.

I've posted two questions in stack overflow here and here, and received good advice, but no solution.

Additionally, I have another program here which opens files just fine with no issues... I have no idea why one is opening files, and the other isn't. (Reminder, NONE of my recent programs are opening files.)

{Update: checking above program before this post, NOW it fails, saying the files don't exist... This program was running yesterday and I made no changes to it. I promise it was working... So why am I having file issues suddenly?}

Just to be sure, I've:

  1. Gone to Build Phases >> Copy Files >> Add File, adding both files to Absolute Path
  2. Checked the spelling of the files and my code

Here is a simple program that should open a text file, then print the contents, which are "testing 1 2 3" - except it doesn't:

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

using namespace std;

int main() {

    fstream inFile;

    string words;

    inFile.open("test.txt");
    if (!inFile) {
        cout << "Failure." << endl;
        exit(1);
    }

    getline(inFile, words);

    cout << words << endl;


    return 0;
}

This program fails every time (as do my others).

Here is my output:

Failure.

Program ended with exit code: 1

Setting a breakpoint at the statement inFile.open("test.txt");, I get:

Printing description of inFile:
(std::__1::fstream) inFile = {
  std::__1::basic_iostream<char, std::__1::char_traits<char> > = {
    std::__1::basic_istream<char, std::__1::char_traits<char> > = {
      std::__1::basic_ios<char, std::__1::char_traits<char> > = {
        std::__1::ios_base = {
          __fmtflags_ = 4098
          __precision_ = 6
          __width_ = 0
          __rdstate_ = 0
          __exceptions_ = 0
          __rdbuf_ = 0x00007fff5fbff4f8
          __loc_ = 0x00007fff7b718c30
          __fn_ = 0x0000000000000000
          __index_ = 0x0000000000000000
          __event_size_ = 0
          __event_cap_ = 0
          __iarray_ = 0x0000000000000000
          __iarray_size_ = 0
          __iarray_cap_ = 0
          __parray_ = 0x0000000000000000
          __parray_size_ = 0
          __parray_cap_ = 0
        }
        __tie_ = 0x0000000000000000
        __fill_ = -1
      }
      __gc_ = 0
    }
    std::__1::basic_ostream<char, std::__1::char_traits<char> > = {
      std::__1::basic_ios<char, std::__1::char_traits<char> > = {
        std::__1::ios_base = {
          __fmtflags_ = 4098
          __precision_ = 6
          __width_ = 0
          __rdstate_ = 0
          __exceptions_ = 0
          __rdbuf_ = 0x00007fff5fbff4f8
          __loc_ = 0x00007fff7b718c30
          __fn_ = 0x0000000000000000
          __index_ = 0x0000000000000000
          __event_size_ = 0
          __event_cap_ = 0
          __iarray_ = 0x0000000000000000
          __iarray_size_ = 0
          __iarray_cap_ = 0
          __parray_ = 0x0000000000000000
          __parray_size_ = 0
          __parray_cap_ = 0
        }
        __tie_ = 0x0000000000000000
        __fill_ = -1
      }
    }
  }
  __sb_ = {
    std::__1::basic_streambuf<char, std::__1::char_traits<char> > = {
      __loc_ = (__locale_ = 0x00007fff7b718c30)
      __binp_ = 0x0000000000000000
      __ninp_ = 0x0000000000000000
      __einp_ = 0x0000000000000000
      __bout_ = 0x0000000000000000
      __nout_ = 0x0000000000000000
      __eout_ = 0x0000000000000000
    }
    __extbuf_ = 0x0000000102001000 ""
    __extbufnext_ = 0x0000000000000000
    __extbufend_ = 0x0000000000000000
    __extbuf_min_ = ""
    __ebs_ = 4096
    __intbuf_ = 0x0000000000000000
    __ibs_ = 0
    __file_ = 0x0000000000000000
    __cv_ = 0x00007fff7b718a40
    __st_ = (__mbstate8 = "", _mbstateL = 0)
    __st_last_ = (__mbstate8 = "", _mbstateL = 0)
    __om_ = 0
    __cm_ = 0
    __owns_eb_ = true
    __owns_ib_ = false
    __always_noconv_ = true
  }
}
(lldb)  

And setting a breakpoint at if(!inFile) ... I get (looks that exact same to me):

  Printing description of inFile:
  (std::__1::fstream) inFile = {
  std::__1::basic_iostream<char, std::__1::char_traits<char> > = {
    std::__1::basic_istream<char, std::__1::char_traits<char> > = {
      std::__1::basic_ios<char, std::__1::char_traits<char> > = {
        std::__1::ios_base = {
          __fmtflags_ = 4098
          __precision_ = 6
          __width_ = 0
          __rdstate_ = 4
          __exceptions_ = 0
          __rdbuf_ = 0x00007fff5fbff4f8
          __loc_ = 0x00007fff7b718c30
          __fn_ = 0x0000000000000000
          __index_ = 0x0000000000000000
          __event_size_ = 0
          __event_cap_ = 0
          __iarray_ = 0x0000000000000000
          __iarray_size_ = 0
          __iarray_cap_ = 0
          __parray_ = 0x0000000000000000
          __parray_size_ = 0
          __parray_cap_ = 0
        }
        __tie_ = 0x0000000000000000
        __fill_ = -1
      }
      __gc_ = 0
    }
    std::__1::basic_ostream<char, std::__1::char_traits<char> > = {
      std::__1::basic_ios<char, std::__1::char_traits<char> > = {
        std::__1::ios_base = {
          __fmtflags_ = 4098
          __precision_ = 6
          __width_ = 0
          __rdstate_ = 4
          __exceptions_ = 0
          __rdbuf_ = 0x00007fff5fbff4f8
          __loc_ = 0x00007fff7b718c30
          __fn_ = 0x0000000000000000
          __index_ = 0x0000000000000000
          __event_size_ = 0
          __event_cap_ = 0
          __iarray_ = 0x0000000000000000
          __iarray_size_ = 0
          __iarray_cap_ = 0
          __parray_ = 0x0000000000000000
          __parray_size_ = 0
          __parray_cap_ = 0
        }
        __tie_ = 0x0000000000000000
        __fill_ = -1
      }
    }
  }
  __sb_ = {
    std::__1::basic_streambuf<char, std::__1::char_traits<char> > = {
      __loc_ = (__locale_ = 0x00007fff7b718c30)
      __binp_ = 0x0000000000000000
      __ninp_ = 0x0000000000000000
      __einp_ = 0x0000000000000000
      __bout_ = 0x0000000000000000
      __nout_ = 0x0000000000000000
      __eout_ = 0x0000000000000000
    }
    __extbuf_ = 0x0000000102001000 ""
    __extbufnext_ = 0x0000000000000000
    __extbufend_ = 0x0000000000000000
    __extbuf_min_ = ""
    __ebs_ = 4096
    __intbuf_ = 0x0000000000000000
    __ibs_ = 0
    __file_ = 0x0000000000000000
    __cv_ = 0x00007fff7b718a40
    __st_ = (__mbstate8 = "", _mbstateL = 0)
    __st_last_ = (__mbstate8 = "", _mbstateL = 0)
    __om_ = 0
    __cm_ = 0
    __owns_eb_ = true
    __owns_ib_ = false
    __always_noconv_ = true
  }
}
(lldb) 
Community
  • 1
  • 1
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52

1 Answers1

0

Short answer is, my project was broken because it was trying to run a project in a path where a previous project already existed. Once I deleted both completely, and started anew, my program worked just fine.

While I made no changes to the original project, I started a new project in the same directory, which then caused all my issues.

NonCreature0714
  • 5,744
  • 10
  • 30
  • 52