0

I want to read in lines from a text file into a 2-d char array but without the newline character.

Example of .txt:

TCAGC
GTAGA
AGCAG
ATGTC
ATGCA
ACAGA
CTCGA
GCGAC
CGAGC
GCTAG

...

So far, I have:

ifstream infile;
infile.open("../barcode information.txt");
string samp;
getline(infile,samp,',');
BARCLGTH = samp.length();
NUMSUBJ=1;
while(!infile.eof())
{
getline(infile,samp,',');
NUMSUBJ++;
}
infile.close();  //I read the file the first time to determine how many sequences
                 //there are in total and the length of each sequence to determine
                 //the dimensions of my array. Not sure if there is a better way?
ifstream file2;
file2.open("../barcode information.txt");
char store[NUMSUBJ][BARCLGTH+1];

    for(int i=0;i<NUMSUBJ;i++)
    {
        for(int j=0;j<BARCLGTH+1;j++)
        {
        store[i][j] = file2.get();
        }
    }

However, I do not know how to ignore the newline character. I want the array to be indexed so that I can access a sequence with the first index and then a specific char within that sequence with the second index; i.e. store[0][0] would give me 'T', but I do not want store[0][5] to give me '\n'.

Also, as an aside, store[0][6], which I think should be out of bounds since BARCLGTH is 5, returns 'G',store[0][7] returns 'T',store[0][8] returns 'A', etc. These are the chars from the next line. Alternatively, store[1][0],store[1][1], and store[1][2] also return the same values. Why does the first set return values, shouldn't they be out of bounds?

3
  • You make a newbie mistake in your loop, in that you loop while not EOF. The eof flag will not be set until a read operation hits EOF, and so you will attempt to get one line to many and also increase NUMSUBJ one to many. Do e.g. while (std::getline(...)) instead. Commented Jul 2, 2013 at 15:51
  • 1
    If you're using C++, I suggest you use std::string and std::vector instead of dealing with char arrays. Much safer and much easier. Commented Jul 2, 2013 at 15:51
  • @JoachimPileborg when I cout NUMSUBJ and BARCLGTH, I am getting correct values. Does having getline(...) within the while loop not count as a read operation? (and yes, I'm sort of new at this stuff, sorry =)!) Commented Jul 2, 2013 at 16:03

1 Answer 1

2

As you're coding in C++, you could do like this instead:

std::vector<std::string> barcodes;

std::ifstream infile("../barcode information.txt");

std::string line;
while (std::getline(infile, line))
    barcodes.push_back(line);

infile.close();

After this the vector barcodes contains all the contents from the file. No need for arrays, and no need to count the number of lines.

And as both vectors and strings can be indexed like arrays, you can use syntax such as barcodes[2][0] to get the first character of the third entry.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, thank you so much! The rest of my program actually needed the values of NUMSUBJ and BARCLGTH which is why I thought I had to count and use char arrays in the first place. I could do barcodes.size() and barcodes[0].size() to extract this information though. Good solution =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.