Hi, everybody. This is my first day here. And I create a new account because I hope to learn more from you.

Here is the my task. I have a text file, it contains a DNA sequence. I want to fragment it into sequences with a definitive length into a single text file. Each new sequence has its own number as its name. The sequence length can be set by the user. So I drive myself yesterday until 2 a.m. to write it. It looks like this:

//Fragmenting genome sequences

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
int FragmentLength;
char fileName[80];  //contain the source DNA sequence file
char newfileName[80];  //created as the target file, with the fragmented sequences
double FileNo;    //numbering the Sequences

cout<<"Please tell the Genome file name:";
cin>>fileName;
cout<<"To which file the fragments should be saved?: ";
cin>>newfileName;
cout<<"How long is a fragment?: ";
cin>>FragmentLength;

char ch;   //as buffer
ifstream fin(fileName);

int pointer;   //used to judge, if the fragment reach the fragment length
pointer=0;  //initializing the pointer
FileNo=1;  //initializing the FileNo
ofstream fout(newfileName);
fout<<fileName<<FileNo<<endl;   //Write the first fragment file name

while (fin.get(ch))
{
ofstream fout(newfileName,ios::app);
fout<<ch;
pointer++;

if(pointer==FragmentLength)
{
FileNo++;   //FileNo increase by 1
ofstream fout(newfileName,ios::app);   
fout<<endl<<fileName<<FileNo<<endl;    //write the new fragment name
pointer=0; //set the pointer back to 0
}
fout.close();
}
fin.close();

cout<<""Fragmentation is finished!";

return 0;
}

Try to compile it and run. It goes. But the first fragment always misses a letter. For example, I enter the fragmentlength 5, in output file, the first fragment has only four letters instead of five, but the other fragments are OK, have really five letters under their names.

HOW CAN THIS HAPPEN?

I have tried to run the loop in my heart, it is ok. I have tried to fix it, and found that, if the statement: pointer++ is put behind the "if" block, the whole problem is gone. I hand in the later version as my homework but am still eager to want to know why.

Thank you for whatever you reply.

Please post the first 3 or 4 lines of the input file.

ATCGGTCATGTCAGTACGTAGTTTGACGACAAGTACGTACCCAGTACGGTAAACCAGTAAACC

This is an input file

line 32: why are you opening the output file for every character read from the input file??? Its already opened on line 28 and that one is never closed, so line 32 attempts to open the same file for output twice, and that will fail.

It must be because of the:
ofstream fout(newfileName,ios::app);
inside your if statement.
You in fact repoen a file that was not closed and it seems to screw up the output.
Cheers,
Pascal.

may I delete both line 33 and 40?

Thank you first for your help.

Hey, man, your Soltuions rock!!!

The Praise from my boss belongs partly to you!!!

Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.