Answers for "how to print the output from 2 files into other file in C++"

C++
0

how to print the output from 2 files into other file in C++

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream file("genome.txt");         //ifstream object to read from file
    string targetSequence = "";          //string to store targetSequence from file
    ifstream file2("genome2.txt");       //ifstream object to read for file 2
    string querySequence = "";           //string to store querysequence from file
    ofstream file3("output.txt");
    string output = "";
    string Line; //variable to read current line from file

    //read till end of file and store it in target Sequence
    while (file >> Line)
    {
        targetSequence += Line;
    }
    //read till end of file and store it in query sequence
    while (file2 >> Line) {
        querySequence += Line;
    }
    while (file3 << Line) {
        output = Line;
    }

    //output the length of target and query sequence
    cout << "\nLength of target sequence: " << targetSequence.length();
    cout << "\nLength of query sequence: " << querySequence.length() << endl;

    int Counter = 0; //variable to store count of matches found

    //Finding the occurences of the given query sequence
    for (int i = 0; i < targetSequence.length(); i++)
    {
        //variable to check if match is found
        bool found = true; //assuming its found initially

        //comparing two strings
        for (int j = 0; j < querySequence.length(); j++)
        {
            //if mismatch occurs make found = false and break
            if (querySequence[j] != targetSequence[i + j])
            {
                found = false;
                break;
            }
        }
        // If querySequence found, print it's index
        if (found)
        {
            cout << querySequence << " found at index " << i << endl;
            Counter++; //increase the count as well
        }
    }

    //if counter is zero print this msg.
    if (Counter == 0)
    {
        cout << querySequence << " not found!" << endl;
    }
    cout << "Counter: " << Counter << endl; // Displaying counter of querySequene
    return 0;
}
Posted by: Guest on October-02-2021

Code answers related to "how to print the output from 2 files into other file in C++"

Browse Popular Code Answers by Language