簡體   English   中英

比較2個文本文件

[英]Comparing 2 text files

我一直在編寫代碼,但遇到了問題。

我有2個文本文件.txt,如下所示:

文件編號。 1(快速)

DOJHLOP01DWJWJ 
TCGATTCTATGGAGGGATGCTGGCAAGGCTCCGGAAGCAGCATCAGCAATTAAAAAAT
DOJHLOP01C1GFF 
TTTACACAGTTGCAAGCCCTGAAGCTTGTGCTTCGATTCTATGGAGGGATGCTGGCAA
DOJHLOP01CAI84 
AGGCGTCGCAGACAGGTTACTTATGTTTGAACATAGTGTTTACACAGTTGCAAGCCCT

文件編號。 2(合格)

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 

如ypu所見,每第二行都是相同的,因此,我想獲取輸出文件,其中包含等效行,即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

我已經使用矢量編寫了代碼,但是出了些問題,因為我得到了一個空白文件作為輸出。 請幫我!

#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;
}

非常感謝您的幫助!

更改fstream out_file; to ofstream out_file;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM