0

更新:我可以解决这个问题。问题可以关闭!(如果您想知道如何:检查解决方案)

我有一个小问题。我最近决定购买 Macbook Air,因为我们必须使用基于 POSIX 的函数,例如 pthread_create、fork 等(并行编程)。这是我第一台装有 MacOS 的笔记本电脑。

我在上面安装了 Xcode 并开始了一个新的 c++ 项目。然后我编写了我的程序并尝试运行它。没有错误并且构建成功,但是当我尝试运行它时,我在最奇怪的地方遇到了 SIGABRT 错误。我不知道为什么会这样,所以我在我的 Windows 桌面上的一个 Eclipse 项目中复制了代码,它工作得很好。你能帮我解决这个问题吗?可能是什么问题?我可能忘了安装什么东西吗?

#include <iostream>
#include <unistd.h>
#include <fstream>
#include <vector>
#include <map>
#include <iterator>
#include <cstdlib>
int main(int argc, const char * argv[])
{
    const char* sequenceFile=argv[1];
    const char* patternFile=argv[2];
    std::ifstream file;
    file.open(sequenceFile,std::ifstream::in);
    std::vector<std::string> k;
    std::string line;
    if(file.is_open()){
        while(std::getline(file,line)){
            k.push_back(line);
        }
    }
    int lengseq=atoi(k.at(0).c_str()); //for example SIGABRT here
    std::string sequence = k.at(1);
    file.close();
    file.open(patternFile,std::ifstream::in);
    k.clear();
    if(file.is_open()){
        while(std::getline(file,line)){
            k.push_back(line);
        }
    }
    std::map<std::string,int> patterns;
    for(unsigned i=1;i<k.size()-1;){
        patterns[k.at(i+1)]=atoi(k.at(i).c_str());
        i+=2;
    }
    //serial
    bool seqCheck = false;
    typedef std::vector<std::pair<std::string, int> > my_vector;
    my_vector patternOccurences;
    typedef std::map<std::string,int>::iterator it_type;
    for(it_type iterator=patterns.begin();iterator!=patterns.end();iterator++){
        for(int i = 1;i<lengseq;i++){
            if(i+iterator->second-1<=lengseq){
                for(int j=0;j<iterator->second;j++){
                    if(sequence.at(j+i-1)==iterator->first.at(j)){
                        seqCheck=true;
                    }
                    else{
                        seqCheck=false;
                        break;
                    }
                }
                if(seqCheck==true){
                    patternOccurences.push_back(std::make_pair(iterator->first,i));
                }
            }
        }
    }
    for(unsigned i =0;i<patternOccurences.size();i++){
        std::cout<<"Pattern: "<<patternOccurences.at(i).first<<" at index "<<patternOccurences.at(i).second-1<<std::endl;
    }

    return 0;

}

那是我正在使用的代码。我很抱歉代码中的混乱。如果您想知道我确实设置了命令行参数,那么这不是问题。

编辑:似乎文件无法打开。我补充说:

else{
        std::cerr<<"Couldn't open file!";
        return 0;
    }

在 if(file.is_open()) 之后,它直接跳到那里。我设置了文件名(带有项目->方案->编辑方案的命令行参数,然后添加为参数,我在项目目录中有文本文件。可能是什么问题?

解决方案:问题是我必须输入文本文件的整个路径。我习惯于在项目目录中拥有文本文件,然后在我的 Windows 桌面上使用 Eclipse 时我不必键入路径(可能是因为工作区)。感谢所有帮助过的人。当我使用 Xcode 时,我会记得从现在开始输入路径。;)

提前谢谢你,我希望你能帮助我。卡祖伊

4

0 回答 0