File I/O Comparing numbers.

I have 3 lines in my file.

34532 2323 43242
523 532652 324
4325 5235 235562

If I getline, each I only have 3 cells.
If I "infile >>" I will have 9 cells.

What I want to do:
The lines are strings. I want to convert each numbers in to integers.

If I use "infile >>" I need to know when is the end of the line and start of the line.
Not every line is going to have 3 numbers, it could have more or less.

If I use getline, the whole line is in a single cell so I don't know how I can convert each numbers.

1
2
3
4
5
	while(infile.good())
	{
		getline(infile, line);
		lotto.push_back(line);
	}
Use std::istringstream. What you do is, send each line into a stringstream, and then use >> with each individual line's stringstream.

Replace the std::istringstream f(file_text); with std::ifstream f("filename");

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
// Example program
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

int main()
{
    std::string file_text =
      "34532 2323 43242\n"
      "523 532652 324\n"
      "4325        5235       235562         11111\n";

    // IMPORTANT: REPLACE WITH 
    // std::ifstream f("filename");
    std::istringstream f(file_text);
    
    std::string line;
    while (getline(f, line))
    {
        std::istringstream iss(line);
        std::cout << "newline: ";
        
        int number;
        while (iss >> number)
        {
            // do your push_back here, or whatever
            std::cout << number << " ";
        }
        std::cout << "\n";
    }
}
Last edited on
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* 
* Convert a text file lines into integers
* Convert each line into integers and store them into a int vector
* Now I just have to make the conversion mechanism shorter....
*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;

int main () {
	ifstream i("example.txt");
	
	vector<string> v;
	string line;
	
	while(i.good())
	{
		getline(i, line);
		v.push_back(line);
	}

	string x, z = "";
	char kind;
	int y = 0, w = 0, u;
	vector<int> vv;
	for(int i = 0; i < v.size();i++)
	{
		x = v[i];
		for(int j = 0;j < x.length();j++)
		{
			kind = x[j];
			if(kind == ' ')
			{
				y = j;
				while(w < y)
				{
					z += x[w];
					w++;
				}
				u = atoi(z.c_str());
				vv.push_back(u);
				z.clear();
			}
		}
		w = 0;
	}
	
	for(int i = 0;i < vv.size();i++)
	{
		cout << vv[i] << endl;
	}
	
  return 0;
}



If you want to test it out

At the end of each line put a space.
This will be nearly as impossible if you are working for a company. So you need to add a space using code instead of manually putting a space by opening up the text file.
Last edited on
I'll try to read your code @Ganado. I need to see how I can compare the numbers I am producing.

thank you for your submission. Because the program I am making I need to compare 100+ lines in a file times how many lines user wants.
Topic archived. No new replies allowed.