reading ints from text file one char at a time into array?

Hello. This is my first post here. I am having a little trouble with my C++ program (using Dev-C++).

The program is supposed to read in a line from a text file into an array and then multiply the array by another int array (which I will get to, later).

I know how to read a line, one char at a time, into an array as a string, but how do you do this using only ints?

If the file has
124
222
333

I want my first array to end up as:
ary[0] = 1;
ary[1] = 2;
ary[2] = 4;

and the second one to contain the second line, so that these two numbers can be multiplied.

Here is my code so far, although I know it is not quite correct. Any help would be much appreciated.

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
#include<iostream>
#include<fstream>
#include<conio.h>

using namespace std;
const int SIZE = 25;

int main() {
	ifstream inFile;
	ofstream outFile;
	char num = '0';
    string num1[SIZE]; 
    string num2[SIZE]; 
    string ans[SIZE];
    
	inFile.open("BigNumbers.txt");
	
	if(!inFile){
	    cout<<"Error opening file.";
	    return 0;
	}
	
	num = inFile.get(); //priming read	
	while(inFile.good()){//loop while extraction from file is possible
	    for(int i = 0; i<SIZE; i++){ //read one line into one array
     	    num1[i] = num; 
            num = inFile.get(); // get another character from file
         }
         //here's where I would put the code to perhaps call a function
         //to convert the contents of the array into ints and save them
         //in another array?
         //I need to have three total, so we can multiply two large numbers
         //(in two arrays) and hold the result in the third one
         //but I have to get the numbers to multiply, one at a time from
         //the file
    }
	   cout<<endl; 
	for (int i =0; i<SIZE; i++){
		cout<<num1[i]<<", ";
		}

	
getch();
return 0;
}
You could try
istream >> acharvariable;
and then convert it to integer using the atoi() function which I believe is in cctype. I can't say for sure however because I'm not too good with file IO. Try it and see.
Damn, another double post...
Last edited on
Thanks for the reply.

This does not work, though.
1
2
3
4
5
6
7
8
9
	while(inFile.good()){//loop while extraction from file is possible
	    for(int i = 0; i<SIZE; i++){ //read one line into one array
   		    //convert char num to int & place in array
   		    numInt = atoi(num);
     	    num1[i] = numInt; 
            num = inFile.get(); // get another character from file
         }

    } 


error: invalid conversion from 'char' to 'const char*'

I'm not sure my C++ instructor would like me to use atoi anyway. Isn't that mostly for C?
Last edited on
Just in case anyone else would like to know, here is what I was supposed to do to get the numbers converted as we add them to the array:

1
2
3
4
5
6
7
8
9
10
11
12
     int cnt = 0;            
         for(int i = 0; i<SIZE; i++){ //read one line into first array
             
             if(num != '\n'){//make sure it's not the end of the line
                 num1[i] = num - '0'; //convert from ascii to int
                 cnt++;//how many digits added to the array?
                 num = inFile.get(); // get another character from file
             }
             else{
                 break;//stop at the end of the line
             }
         }


//yes I know it's over-commented but it is for a class.
Topic archived. No new replies allowed.