How do I traverse through a text file?

I want to traverse through each line. All I want to do is compare a user input of 4 numbers with each line of numbers from the file. If it matches the user wins.

Example of my text file
25 45 78 45
12 45 32 52
(I have over 100 lines in my text files, I will be adding more.)

In this file the position of 2, what is the position?
It's not an array, I can always put each line into a vector and clear it and move on to the next line.

This is not a homework problem, I just want to experience a "workplace database" type of code. Also I want to learn File I/O.
Keep checking and editing the text file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>

int main() {
    std::ifstream fin("filename");
    
    int ua, ub, uc, ud;
    std::cout << "Enter your four numbers: ";
    std::cin >> ua >> ub >> uc >> ud;

    int line = 0;
    int a, b, c, d;
    while (fin >> a >> b >> c >> d) {
        ++line;
        if (a == ua && b == ub && c == uc && d == ud) {
            std::cout << "Match on line " << line << '\n';
            break; // if you only want the first match then break here
        }
    }
}

@seungyeon

Does the order of user input numbers have to match the order of numbers in the line of 4 numbers being read?

ex:
25 45 78 45 // Read in line

user inputs
45 78 25 45

Does not win even though they are the same numbers but out of order.
If the order doesn't matter, you can read the numbers into arrays and sort them before comparing.

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>
#include <algorithm>

int main() {
    std::ifstream fin("filename");
    
    int u[4];
    std::cout << "Enter your four numbers: ";
    std::cin >> u[0] >> u[1] >> u[2] >> u[3];
    std::sort(&u[0], &u[4]);

    int line = 0;
    int f[4];
    while (fin >> f[0] >> f[1] >> f[2] >> f[3]) {
        std::sort(&f[0], &f[4]);
        ++line;
        if (f[0] == u[0] && f[1] == u[1] && f[2] == u[2] && f[3] == u[3]) {
            std::cout << "Match on line " << line << '\n';
            break; // if you only want the first match then break here
        }
    }
}

If the order does matter and the maximum number is relatively small (the sample input suggests 2 digit numbers), then you could put the numbers into a bitset<100> and then compare - no sorting required.

However, with only 4 numbers, it's not clear if this would be faster. And it's certainly debatable whether it would be clearer code. That's up to you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main()
{
   const int N = 4;          // number of digits
   const int MAX = 100;      // each digit in range 0-MAX
   int value;
   string line;

   // Target numbers
   vector<int> target(1+MAX,0);
   cout << "Enter " << N << " numbers, in the range 0 - " << MAX << ":  ";
   for ( int i = 0; i < N; i++ )
   {
      cin >> value;
      target[value]++;
   }

   // Numbers to check
// ifstream in( "input.txt" );
   stringstream in( "25 45 78 45\n"
                    "12 45 32 52" );
   while( getline( in, line ) )
   {
      vector<int> temp = target;
      stringstream ss( line );
      int matches = 0;
      for ( int i = 0; i < N; i++ )
      {
         ss >> value;
         if ( temp[value] > 0 )
         {
            matches++;
            temp[value]--;                    // repeats allowed
         }
      }
      cout << "Line: " << line << "    Matches: " << matches << '\n';
   }
}


Enter 4 numbers, in the range 0 - 100:  45 45 32 12 
Line: 25 45 78 45    Matches: 2
Line: 12 45 32 52    Matches: 3



Last edited on
Topic archived. No new replies allowed.