Reading a file of integers

First, thanks for your assistance.

Next, I am trying to read individual integers from a file to an array. The book[1] I have says get(): "Returns the next character extracted from the input stream as an int." So, I suspected it to work when considering integers.

I have read many "suggestions," including "stringstream" which I believe to be overkill if get() is supposed to return an integer. Although I have read many suggestions, nothing has worked. For this reason, I am posting my code. I am a beginner.

In my file, I have 12345678910. What I currently get back when I run the below code is:
239
187
191
49
50
51
52
53
54
55
56
57
49
48
-1



References:

[1] Bronson, Gary J.. C++ for Engineers and Scientists (Page 480). Cengage Textbook. Kindle Edition.

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

void getfile(int [], int); 
void maxcheck(int[], int); 


int main()
{
	const int SIZE = 1500;
	int series[1500]; 

	getfile(series, SIZE); 

    return 0;
}

void getfile(int series[], int size)
{
	//string filename = "C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\Projects\\Project Euler\\8.PE. LargestProductInASeries\\PE.File.txt"; 
	ifstream inFile;
	ofstream outFile; 
	int i; 
	int count = 0; 
	int num = 0;
	int sum = 0; 
	int x; 
	string line; 


	inFile.open("C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\Projects\\Project Euler\\8.PE. LargestProductInASeries\\PE.File.txt", ios::in);

	if (inFile.fail())
	{
		cout << "\nThe file was not successfully opened."
			 << "\n Please check that the file currently exists."
			 << endl; 
		exit(1); 
	}



	for (i = 0;inFile.good(); i++)
	{
		series[i] = inFile.get();
		cout << series[i] << '\n';
	}


}
You read a character but store it as an int. The digit 1 has the ascii code 49 so your array will store 49 as an int.
To convert your character to an int use series[i] = inFile.get() - '0';
Thanks,

That did change part of my output to 12345678910, but I still get:
191
139
143
1
2
3
4
5
6
7
8
9
1
0
-49

and I only have 12345678910 in my text file. You have any idea why? I know that there could be hidden characters that get() is picking up. Is that the case? If so, how can I filter them? should I use an "if". I would rather read a clean input. I know getline() reads differently than get() but I don't need a string, I need integers.
Hello,

I changed my code to use atoi(). I am still getting odd output: large negative numbers, symbols, a zero in between my desired integer, and my desired integers-12345678910.

I need to get rid of the large negative numbers, symbols, and zeros. I can, if I have to, filter all large negative numbers to the top (I have done this with a vector recently and figured it out on my own! :) ). I don't want my code to be so bloated, and I would still have the problems with the symbols. I have to be doing something wrong but I have no idea what I am doing wrong. Here is the new code.

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
60
61
62
63
64
65
66
67
68
69
70



#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream> 
#include <string>
using namespace std; 

void getfile(int [], int); 
void maxcheck(int[], int); 


int main()
{
	const int SIZE = 1500;
	int series[1500]; 

	getfile(series, SIZE); 

    return 0;
}

void getfile(int series[], int size)
{
	//string filename = "C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\Projects\\Project Euler\\8.PE. LargestProductInASeries\\PE.File.txt"; 
	ifstream inFile;
	ofstream outFile; 
	char ser[1500]; 
	int i,j; 
	int count = 0; 
	int num = 0;
	int numI = 0; 
	int sum = 0; 
	int x; 
	string line; 
	int series1[1500]; 


	inFile.open("C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\Projects\\Project Euler\\8.PE. LargestProductInASeries\\PE.File.txt", ios::in);

	if (inFile.fail())
	{
		cout << "\nThe file was not successfully opened."
			 << "\n Please check that the file currently exists."
			 << endl; 
		exit(1); 
	}



	for (i = 0, j = 0; inFile.good(); i++, j++)
	{
		ser[i] = inFile.get();
		cout << ser[i] << "  " << endl; 
		
		if (ser[i] > 0)
		{
			series[i] = atoi(ser);
		}

		cout << series[i] << "   ";
	}

	inFile.close(); 
}



My output:


-858993460 ╗
-858993460 ┐
-858993460 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 1
0 0
0  
-858993460
I caught this error:

1
2
3
4
5
6
7
8

if (ser[i] > 0)
		{
			series[i] = atoi(ser);
		}

		cout << series[i] << "   ";


I changed it. After changing, I get the output seen at the end.

1
2
3
4
5
6
7
8
9

if (atoi(ser) > 0)
		{
			series[i] = atoi(ser);
		}

		cout << series[i] << "   ";



Output:


-858993460 ╗
-858993460 ┐
-858993460 1
-858993460 2
-858993460 3
-858993460 4
-858993460 5
-858993460 6
-858993460 7
-858993460 8
-858993460 9
-858993460 1
-858993460 0
-858993460  
-858993460

If needed, I can swap all negative numbers to the end, but I want a cleaner solution and no symbols.
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream> 
#include <string>

using namespace std;

int getfile(int[], int);
void maxcheck(int[], int);


int main()
{
  const int SIZE = 1500;
  int series[1500];

  int num = getfile(series, SIZE);
  cout << num << " numbers read.\n";
  for (int i = 0; i < num; i++)
  {
    cout << "series[" << i << "] = " << series[i] << "\n";
  }
  system("pause");
  return 0;
}

int getfile(int series[], int size)
{
  ifstream inFile("numbers.txt");
  if (!inFile)
  {
    cout << "\nThe file was not successfully opened."
      << "\n Please check that the file currently exists."
      << endl;
    exit(1);
  }
  char ch;
  int  numRead = 0;
  while (inFile.get(ch) && numRead < size)
  {
    series[numRead] = ch - '0';
    numRead++;
  }

  return numRead;
}

OUTPUT

11 numbers read.
series[0] = 1
series[1] = 2
series[2] = 3
series[3] = 4
series[4] = 5
series[5] = 6
series[6] = 7
series[7] = 8
series[8] = 9
series[9] = 1
series[10] = 0
Press any key to continue . . .

Thanks. I discovered that some of the negative numbers and the symbols were characters in the text file. I used your original suggestion which is so simple. I thank you greatly.
Last edited on
Topic archived. No new replies allowed.