dnadri
dnadri

Reputation: 173

Read .txt files from C++ program in Xcode

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?

#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

Upvotes: 3

Views: 15418

Answers (3)

CHardnett
CHardnett

Reputation: 164

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

Upvotes: 4

whitfin
whitfin

Reputation: 4629

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

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:

// 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::vector anyway, because vectors are just generally easier to work with and there's no reason not to in this situation.

Upvotes: 0

fatihk
fatihk

Reputation: 7919

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:

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

Upvotes: 0

Related Questions