Converting a string from getline(fileIn, Cstring) to integers.


Hello,

I have read many suggestions, I actually copied one, and I cannot convert from file of strings to integers. If I create my own set of integers (mystring) within the IDE, the conversion works nicely. So, I am messing up something wrt the "fileIn" process.

I need to convert the file from a line of strings to integers that I will eventually import into an array or a vector for analysis. The numbers in the file are "23, 34 56" integers and with a single space between them.

Wen I run the code, I get nothing unless I am streaming directly from filein to cout.

Here is my code. By the way, I am new to C++ and teaching myself.


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
#include "stdafx.h"
#include <iostream>
#include <fstream> 
#include <iomanip>
#include <cstdlib> // for exit. 
#include <cmath>
#include <string>
#include<sstream>
using namespace std; 


void inOut(ifstream&, int [20][20]); 

int main()
{
	int Grid[20][20]; 
	ifstream inFile; 

	inOut(inFile, Grid); 

	return 0; 
}

void inOut(ifstream& fileIn, int Grid[20][20])
{
	string filename = "C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\Projects\\Project Euler\\11.PE.Largest Product Grid\\11.PE.Largest Product Grid.dat";
	int num = 0; 
	int i, j; 
	string Cstring;
	string mystring = "45 23 56 78 98"; 

	fileIn.open(filename.c_str(), ios::in); 
	if (fileIn.fail())
	{
		cout << "Sorry, your file did not open."
			<< "Please check to make sure the file exists."; 
		exit(1); 
	}

	while (getline (fileIn, Cstring)) 
	{
		istringstream iss(Cstring); 

		while (iss >> num)
		{
			cout << num << endl; 
		}
	}

		
} 






Last edited on
For a second, after reading, I thought it was supposed to be outfile. I am very new to C++. In fact, I am teaching myself and the only language I know so far.
Last edited on
The numbers in the file are double integers and with a single space between them.

double integers? it can be either double or integer but in any case why go through the std::string route instead of reading the numbers from the file into double or integer variables directly?
Yeah, bad choice of words on my behalf. I meant "23" instead of "1" or "123".

I believe I also mentioned that I am new to C++. Can you explain your statement a little more. How can I read an integer? I am aware of get() (character) and getline(), and extraction.
How can I read an integer?
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
#include <iostream>
#include <vector>
#include <fstream>

int main()
{
   std::ifstream inFile{"D:\\test.txt"};
   std::vector<int> myVec{};
   int num{};
   while (inFile >> num)
   {
      myVec.push_back(num);
   }
   for (const auto& elem : myVec)std::cout << elem << "\n";
   //http://en.cppreference.com/w/cpp/language/range-for
}
/*
Input File: 45 23 56 78 98
Output:
45
23
56
78
98
*/
I tried something like that and failed. Let me try again. I used:

1
2
3
4
5
6
7
8
i = 0; 
while ( i < 400)
	{
		fileIn >> num; 
		arrayNum[i] = num; 
		i++;
		cout << arrayNum[i] << endl; 
	}
Last edited on
arrayNum[i] = num;
make sure you allocate sufficient memory for the array before writing into it
I just used your suggestion and had a blank screen. I forgot to mention, sorry, that the begining of my file has some type of symbols for some reason. Could the symbols, something that might have happened from my copy and paste, be causing the problem. I will create another text file and see.

1
2
3
4
5
6
7
while (fileIn >> num)
	{
		myVec.push_back(num);
	}

	for (const auto& elem : myVec)std::cout << elem << "\n";
gunnerfunner:

I just created a text file myself, did not copy and paste, and it worked. It is a lot of numbers and I would rather copy and paste but I might have to enter each one by one because some symbols were inserted before my first number.
Topic archived. No new replies allowed.