简体   繁体   English

比较2个文本文件

[英]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 : 我有2个文本文件.txt,如下所示:

File no. 文件编号。 1 (fasta) 1(快速)

DOJHLOP01DWJWJ 
TCGATTCTATGGAGGGATGCTGGCAAGGCTCCGGAAGCAGCATCAGCAATTAAAAAAT
DOJHLOP01C1GFF 
TTTACACAGTTGCAAGCCCTGAAGCTTGTGCTTCGATTCTATGGAGGGATGCTGGCAA
DOJHLOP01CAI84 
AGGCGTCGCAGACAGGTTACTTATGTTTGAACATAGTGTTTACACAGTTGCAAGCCCT

File no. 文件编号。 2 (qual) 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 

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. 如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

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; 更改fstream out_file; to ofstream out_file; to ofstream out_file;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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