Reading integers from a text file and fork() a child process

First time post and I am out of my league, so I apologize up front. I have reviewed C++ for Beginners, been on cplusplus, dreamincode, sanfoundry, tutorialpoint, learncpp and clicked every link yahoo, Google, and Bing will provide. I am trying to get some guidance for this: Create a random array of numbers (did that) and search it with the query integers from input.txt. Fork a child process for each query. The file input.txt is this:
5
13
24
6
17
20
1
51
36
42
2
19
67
35
64
91
96
84
72

I have to count the number of times each integer appears in the array (I have that, too). I could hard code each in to the algorithm I have, but I am asked to pass the values from input.txt by reading the file, and I cannot figure out how to do that. Also, if there are any suggestions for the fork(), I would take those also. Thank you!!! (I do realize I need to move on from "namespace" - sorry. I am getting there slowly.) Thanks, again!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    #include <cstdlib> 
    #include <ctime> 
    #include <iostream>
    #include <algorithm>
     
    using namespace std;
     
    int main() 
    { 
        srand((int) time(0));
        int size;
        int random_integer[1000]; 
        for(int i=0; i<size; i++)
        { 
            random_integer[i] = (rand() % 101); 
            //cout << random_integer[i] << endl; 
        }
     
        int mycount = std::count (random_integer, random_integer+1000, 46);
        cout << "46 appears " << mycount << " times. \n";
     
        return 0;
    }
Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream infile("input.txt");
    cout << "Enter search no: \n";
    int num;
    cin >> num;
    int counter{};
    while(infile)
    {
        int temp{};
        infile >> temp;
        if(num == temp)
        {
            counter++;
        }
    }
    cout << "Number appears " << counter << " times in the file \n";
}
Thank you! Very slick and it compiled nicely in cpp.sh. I need to go in reverse and have the input characters from the text file search my random number array in my program. Thanks to your example, however, it gave me a clue and I am working it out. It is slop at the moment, so I will post it when I get it cleaned up. I wanted to say thanks for the direction!
Topic archived. No new replies allowed.