在 Xcode 中从 C++ 程序中读取 .txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15906768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:10:31  来源:igfitidea点击:

Read .txt files from C++ program in Xcode

c++xcodefile-io

提问by dnadri

I have been struggling on getting my C++ program to read my .txt file from Xcode. I even tried putting the .txt file in the same directory of my Xcode C++ program but it won't read from it successfully. I am trying to fill the dnaData array with all the nucleotides in the file, so I only have to read it once and then I can just operate on that array. Below is just a part of my code that handles the file. The idea of the whole program is to write a program that reads an input file (dna.txt) containing DNA sequences, analyzes the input in various ways, and outputs several files containing various results. The maximum number of nucleotides (see Table 1) in the input file will be 50,000. Any suggestions please?

我一直在努力让我的 C++ 程序从 Xcode 读取我的 .txt 文件。我什至尝试将 .txt 文件放在我的 Xcode C++ 程序的同一目录中,但它无法成功读取。我试图用文件中的所有核苷酸填充 dnaData 数组,所以我只需读取一次,然后我就可以对该数组进行操作。下面只是我处理文件的代码的一部分。整个程序的思想是编写一个程序,读取一个包含DNA序列的输入文件(dna.txt),以各种方式分析输入,并输出包含各种结果的几个文件。输入文件中的最大核苷酸数(见表 1)为 50,000。请问有什么建议吗?

#include <fstream>
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

const int MAX_DNA = 50000;

// Global DNA array. Once read from a file, it is
// stored here for any subsequent function to use
char dnaData[MAX_DNA];

int readFromDNAFile(string fileName)
{
int returnValue = 0;

ifstream inStream;
inStream.open(fileName.c_str());

    if (inStream.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    if (inStream.good())
    {
        char nucleotide;
        int counter = 0;
        while ( inStream >> nucleotide )
        {
            dnaData[counter] = nucleotide;
            counter++;
        }
        returnValue = counter;
    }

    inStream.close();
    return returnValue;
    cout << "Read file completed" << endl;

} // end of readFromDNAfile function

回答by CHardnett

I suspect the problem here is not with the C++ code, but with the file location. In Xcode, the binary programs are built in an Executables location. You have to set up the build phases to copy your input file to the Executables location. See this Apple Documentation

我怀疑这里的问题不在于 C++ 代码,而在于文件位置。在 Xcode 中,二进制程序构建在 Executables 位置。您必须设置构建阶段以将输入文件复制到 Executables 位置。请参阅此Apple 文档

回答by whitfin

I did something like you're trying to do recently using a vectorlike so:

我做了一些你最近尝试做的事情,vector就像这样:

vector<string> v;
// Open the file
ifstream myfile("file.txt");
if(myfile.is_open()){
    string name;
    // Whilst there are lines left in the file
    while(getline(myfile, name)){
        // Add the name to the vector
        v.push_back(name);
    }
}

The above reads the name stored on each line of the file and adds them to the end of the vector. So if my file was 5 names, the following would happen:

以上读取存储在文件每一行的名称并将它们添加到向量的末尾。因此,如果我的文件有 5 个名称,则会发生以下情况:

// Start of file
Name1    // Becomes added to index 0 in the vector
Name2    // Becomes added to index 1 in the vector
Name3    // Becomes added to index 2 in the vector
Name4    // Becomes added to index 3 in the vector
Name5    // Becomes added to index 4 in the vector
// End of file

Try that and see how it works for you.

试试看,看看它如何为你工作。

Even if you don't go with the way shown above, I'd still recommend using std::vectoranyway, because vectors are just generally easier to work with and there's no reason not to in this situation.

即使你不采用上面显示的方式,我仍然建议使用std::vector,因为向量通常更容易使用,在这种情况下没有理由不使用。

回答by fatihk

if each line contains one character, then this means that you are also reading end-line character ('\n') into the DNA array. In this case, you can do:

如果每一行包含一个字符,那么这意味着您也在将行尾字符 ('\n') 读入 DNA 数组。在这种情况下,您可以执行以下操作:

while ( inStream >> nucleotide )
{
        if(nucleotide  == '\n')
        {
              continue;
        }
        dnaData[counter] = nucleotide;
        counter++;
}