我一直在努力讓我的C++程序來讀取我的Xcode的.txt文件。 我甚至試圖將.txt文件放在我的Xcode C++程序的同一目錄中,但它不會成功讀取它。我試圖用文件中的所有核苷酸填充dnaData數組,所以我只需要讀一次,然後我就可以對該數組進行操作。以下是處理文件的代碼的一部分。整個程序的想法是編寫一個程序,讀取包含DNA序列的輸入文件(dna.txt),以各種方式分析輸入,並輸出包含各種結果的多個文件。輸入文件中核苷酸的最大數量(見表1)將爲50,000。 有什麼建議嗎?閱讀txt文件從C++程序在Xcode
#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
的輸出是什麼,是節目結束芯片成功或失敗? – 2013-04-09 16:16:20
使用'std :: vector'。沒有必要在這裏使用固定數組,只會使事情更加複雜。 – 2013-04-09 16:21:46
「但它不會成功讀取」不是非常具體。請解釋錯誤/問題/意外輸出/等。你得到。 – 2013-04-09 16:24:29