我一直在努力让我的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
std::vector
。这里不需要使用固定数组,这只会让事情更加复杂。 - lethal-guitar