0

So basically I want to find in my text file "NAPIS.txt" every line which has 2 letter "A" in it and then append number of these lines and lines themselves to new text file "wynika.txt". For some reason these lines append multiple times and there is too much of them. here is code:

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

using namespace std;
vector <string> t;

int main()
{
    ifstream plikwe("NAPIS.txt");
    ofstream pliwky_a("wynika.txt");
    ofstream pliwky_b("wynikb.txt");
    string temp;
    int licz=0;
    int licznik=0;
    while(!plikwe.eof())
    {
        plikwe>>temp;
        t.push_back(temp);
    }
    for(int i=0;i<t.size()-1;i++)
    {
        cout<<t[i]<<endl;
    }
    for(int i=0;i<t.size()-1;i++)
    {
        licz=0;
        for(int j=0;j<t[i].size();j++)
        {
             if(t[i].at(j)=='A')
             {
                 licz++;
             }
             if(licz==2)
             {
                 licznik++;
                 pliwky_a<<t[i]<<endl;
             }

        }
    }
    pliwky_a<<licznik<<endl;
    cout<<licznik<<endl;

    return 0;
}

NAPIS text file

wynika text file

cray 1234
  • 9
  • 2
  • 1
    Move the `if(licz==2) { ... }` block to _after_ the `for` loop. – 001 Nov 02 '21 at 13:56
  • 3
    First of all please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) Then the loops `for(int i=0;i – Some programmer dude Nov 02 '21 at 13:57
  • 2
    Note also that the `>>` operator on `std::string` does not input a *line*, it inputs a *word*. You might want to use [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) instead. – Fred Larson Nov 02 '21 at 14:00

1 Answers1

0

The below given complete working example shows what you want. It reads "NAPIS.txt" line by line and then checks if that line contains the character 'A' exactly 2 times or not. If the the line just read contains the character 'A' exactly 2 times then that line with corresponding line number is written to "wynika.txt" .

#include <iostream>
#include <fstream>
#include <algorithm>
int main()
{
    std::ifstream inputFile("NAPIS.txt");
    
    std::string line; //this will be used to read the line from the file 
    
    int currentLineNum = 0;//this will be used to write the current line number and write in output file
    if(inputFile)
    {
        std::ofstream outputFile("wynika.txt");
        
        while(std::getline(inputFile, line, '\n'))//read into line
        {
            ++currentLineNum;
            //check if line contains 2 'A' or not. 
            if(std::count(line.begin(), line.end(), 'A') == 2)
            {
                //std::cout<<"A found"<<std::endl;
                
                //write the line number into wynika.txt 
                outputFile << currentLineNum << ' ';  //you can skip this if you don't want to write the line number into the output file
                
                //write the line into wynika.txt
                outputFile << line << '\n';
                
                
            }
        }
        //dont forget to close the file 
        outputFile.close();
        
    }
    else 
    {
        std::cout<<"Input file cannot be opened"<<std::endl;
    }
    inputFile.close();
    return 0;
}

The output of the above program can be seen here.

If you don't want to write the line number then you can comment(that is remove) the statement:

outputFile << currentLineNum << ' ';

from the above code.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • The purpose of the program in the question is to only write "words" which has exactly two (consecutive or non-consecutive) letters `'A'` in them. Your program write all "words" which has at least one `'A'` in them (one, two, three or more). – Some programmer dude Nov 02 '21 at 14:38
  • @Someprogrammerdude Okay when i read "every line which has 2 letter "A" in the OP's question i misunderstood the question since he mentioned "line" and not words. Also his/her use of "2 letter "A" " was not clear enough for me. – Jason Nov 02 '21 at 14:43
  • @Someprogrammerdude I have updated the code according to what you said. Check it out. Now the program only write those lines which have exactly 2 `A` in them. – Jason Nov 02 '21 at 14:51