Re: C++ and DNA sequences
Making such a program can be very simple if you you use standard tools like the <fstream> library for file I/O, and the Standard Template Library classes, like std::string.
The insertion operator >> reads one word from the screen (in the case of an istream object like cin) or from a file (in the case of an ifstream object) into a target string (if you use std::string, the number of characters that it can read in is practically unlimited). So assuming that your sequence name and the actual sequence are separated by spaces (or newlines, tabs etc.), you can read them in like this:
Code:
ifstream fin; // ifstream object for reading file
fin.open("myfile.txt"); // open the file
std::string name1, name2, sequence1, sequence2;
fin >> name1; // stores name of sequence 1
fin >> sequence 1; // stores sequence 1
fin >> name2; // stores name of sequence 2
fin >> sequence2; // stores sequence 2
That of course is assuming that there are no spaces within each sequence of characters. If there are, you may need multiple strings to store each segment and concatenate them after using the += operator. Or, use the getline() function instead, with the > character marking the beginning of the next sequence as the character at which to stop reading.
Now, say you want to erase the > from the string name, simply do:
Code:
name1.erase(name1.begin()); // erases the first character of the string
Writing the new strings to a file is even simpler than inputting them: you use the same syntax as if you'd be outputting to the screen, except replace cout with an ofstream object:
Code:
ofstream fout;
fout.open("output_file.txt");
fout << name1 << " " << sequence1 << " " << name 2 /* etc */;
As you can see, a program like this is very simple to make. As long as you are familiar with the basic ways of handling an std::string, you can do things like this and even more complex stuff, very easily. This is a good reference for working with strings, detailing what each member function does. Note that the page is part of a larger site that describes the entire STL, not just the string class.