简体   繁体   中英

Comparing 2 text files

I've been writing a code, and I have an issue.

I have 2 text files .txt, which look like that :

File no. 1 (fasta)

DOJHLOP01DWJWJ 
TCGATTCTATGGAGGGATGCTGGCAAGGCTCCGGAAGCAGCATCAGCAATTAAAAAAT
DOJHLOP01C1GFF 
TTTACACAGTTGCAAGCCCTGAAGCTTGTGCTTCGATTCTATGGAGGGATGCTGGCAA
DOJHLOP01CAI84 
AGGCGTCGCAGACAGGTTACTTATGTTTGAACATAGTGTTTACACAGTTGCAAGCCCT

File no. 2 (qual)

DOJHLOP01DWJWJ 
32 31 29 26 30 27 32 32 31 30 28 26 26 28 27 19 26 31 24 32 31 30 25 25 29 
DOJHLOP01C1GFF
28 28 16 26 31 32 31 28 32 31 27 30 31 29 23 32 29 28 16 31 31 29 24 32 31 
DOJHLOP01CAI84 
31 30 25 30 27 24 31 31 31 32 32 31 30 24 27 21 31 27 32 31 30 25 25 28 31 

As ypu can see, every 2nd line is the same, and as a result of I want to get the output file, that contains the equivalent lines, which are different, i.ex.

TCGATTCTATGGAGGGATGCTGGCAAGGCTCCGGAAGCAGCATCAGCAATTAAAAAAT
32 31 29 26 30 27 32 32 31 30 28 26 26 28 27 19 26 31 24 32 31 30 25 25 29

I've written the code, using vector, but something is wrong, because I get a blank file as the output. Please, help me!

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
        string fasta,qual;
        vector <string> v1,v2;
        ifstream file;
        fstream out_file;
        int i;

        cout <<"insert the name of fasta file"<<endl;
        cin>>fasta;
        cout<<"instert the name of qual file"<<endl;
        cin>>qual;

        file.open(fasta.c_str(),ifstream::in);
        while(getline(file,fasta))
        {
            v1.push_back(fasta);
        }
        file.close();

        file.open(qual.c_str(),ifstream::in);
        while(getline(file,qual))
        {
            v2.push_back(qual);
        }
        file.close();

        out_file.open ("out_file.txt");

        for(i=0;i<v1.size();i++) //assuming the qual and fasta have the same numer of lines
        {
                if(v1[i]!=v2[i]) //if two lines are different
                {
                        out_file<<v1[i]; //write this line into the out_file
                        out_file<<'\n';
                        out_file<<v2[i]; //write this line into the out_file
                        out_file<<'\n';
                }

        }

        out_file.close();

        return 0;
}

Thanks a lot for help!

change fstream out_file; to ofstream out_file;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM