文件在多个程序中连续无法打开

时间:2015-12-05 01:34:17

标签: c++ xcode file c++11 file-handling

上周,我无法在Xcode中打开文件。我想知道它为什么不起作用,以及如何解决它。

我已经在堆栈溢出herehere中发布了两个问题,并收到了很好的建议,但没有解决方案。

此外,我还有另一个程序here可以正常打开文件而没有任何问题...我不知道为什么打开文件,另一个不是。 (提醒,我最近的程序没有打开文件。)

{更新:在此帖之前检查上述程序,现在它失败了,说文件不存在......这个程序正在运行昨天我做了 更改。我保证它正在工作......那么为什么我突然出现文件问题?}

我敢肯定:

  1. 建立阶段>>复制文件>>添加文件,将两个文件添加到绝对路径
  2. 检查了文件和我的代码的拼写
  3. 这是一个简单的程序,应该打开一个文本文件,然后打印内容,这是"测试1 2 3" - ,但不是

    #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;
    }
    

    这个程序每次都失败了(和我的其他人一样)。

    这是我的输出:

    Failure.

    Program ended with exit code: 1

    在语句inFile.open("test.txt");处设置断点,我得到:

    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)  
    

    if(!inFile)处设置断点...我得到(看起来与我完全相同):

      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) 
    

1 个答案:

答案 0 :(得分:0)

简短的回答是,我的项目被打破了,因为它试图在以前的项目已经存在的路径中运行项目。一旦我完全删除了,并重新开始,我的程序工作得很好。

虽然我没有对原始项目进行任何更改,但我在同一目录中启动了一个新项目,这导致了我的所有问题。