Reading integer from file, in digits

Jun 29, 2012 at 2:43pm
Alright, so I have a file, filename.dtbf, that I create in my program the first time the program is run. The file is there so that when the program is run a second time, it will remember certain variables the user previously defined. For this, I am using ones and twos. (One = Yes, Two = no.) So lets say we have this:

In the file filename.dtbf:
111122

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 <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include "functions.h"

using namespace std;

int main{

int a;
int b;
int c;
int d
int e;
int f;

ifstream readFile;
readFile.open("filename.dtbf");

// Here is the Question

}


How do I make it so each digit of the big number in the file goes to a separate variable?
So in this case of numbers in the file it would end up being:

a == 1;
b == 1;
c == 1;
d == 1;
e == 2;
f == 2;
Jun 29, 2012 at 5:58pm
Many options. One would be to read them in as char, so that one at a time is read in, and then convert that char to a number.
Jun 29, 2012 at 11:59pm
Many options. One would be to read them in as char, so that one at a time is read in, and then convert that char to a number.


Can you please build on that?? Sample code??
Jun 30, 2012 at 10:44pm

1
2
3
4
ifstream inputFile("someFile");
char c;
cin >> c;
// now convert the single char c to an int 

Jul 1, 2012 at 12:51am
Alright. So there is a file, filename.dtbf. It contains this: "111122". I want, each individual digit of that number, to be in a separate int. (a, b, c, d, e, f) resulting in a=1, b=1, c=1, d=1, e=2, f=2. (Likewise, if it was "122121" in filename.dtbf a=1, b=2, c=2, d=1, e=2, f=1).


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 <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include "functions.h"

using namespace std;

int main{

int a;
int b;
int c;
int d
int e;
int f;

ifstream readFile;
readFile.open("filename.dtbf");

// Here is the Question

}


I want a,b,c,d,e,f to all get a separate digit of whatever six digit combination of 1's and 2's there are in filename.dtbf.

BACKSTORY:
This file is generated by the user from the program, setting different variables of how they want to use my program. Then, so the program remembers these settings, it reads the file, and sets the variable to the digits in the file. So, a,b,c,d,e,f can be used in if statements to run different functions the user wants them to be run.
Last edited on Jul 1, 2012 at 12:56am
Jul 1, 2012 at 2:35am
Alright, I figured it out for myself, using a little math. Here is the code I used that worked:

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
int numbers;
ifstream readfile;
readfile.open("filename.dtbf");
readfile >> numbers;
int tempnumbers = numbers;

int a,b,c,d,e,f;
              
a = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << a;
b = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << b;
c = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << c;
d = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << d;
e = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << e;
f = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << f;
printf("Done.\n");


Output:
221111
(Note: the number in the file filename.dtbf was 111122)

All along, I was trying to do something hard, when it was something easy. I just needed to separate a six digit number into separate digits, and that is one way how to do it.

PROBLEM SOLVED.
Jul 1, 2012 at 12:56pm
What happens if the input number has a different number of digits? This code allows the input to contain an unknown, changeable number of digits. It reads each individual digit as a char and stores it as an individual int in a vector.

1
2
3
4
5
6
7
8
9
10
11
ifstream readfile;
readfile.open("filename.dtbf");

char inputDigit;
vector<int> allTheDigits;

while (readfile>>inputDigit(
{
  allTheDigits.pushback( inputDigit - 48);
}
// Now have all the digits stored individually in the vector as int values 


Even easier, however, would be for you to store each int on a separate line when you write them out. Then you could just read in each one as an int and you wouldn't have to do anything like this at all. You could just read them in.
Last edited on Jul 1, 2012 at 12:58pm
Jul 1, 2012 at 1:54pm
Think it just like this
int n = array();
n++ = result++;

i hope you understand it a bit, or read this, i found by googling http://less4us.blogspot.com/2012/06/ccpp-hello-world-application-for.html
Jul 1, 2012 at 5:48pm
What happens if the input number has a different number of digits? This code allows the input to contain an unknown, changeable number of digits. It reads each individual digit as a char and stores it as an individual int in a vector.


It is impossible for there to be any more or less than 6 digits. The function that creates the file will always put in six digits, based on the things that the user puts into the console.

Even easier, however, would be for you to store each int on a separate line when you write them out. Then you could just read in each one as an int and you wouldn't have to do anything like this at all. You could just read them in.


I need each digit to be in a separate int. Not as a whole, if I wanted them in a whole I could just read them in
1
2
3
4
ifstream read("filename");
int number;
read >> number;
return 0;




Think it just like this
int n = array();
n++ = result++;

i hope you understand it a bit, or read this, i found by googling http://less4us.blogspot.com/2012/06/ccpp-hello-world-application-for.html


My question is already solved. And, that link leads to a hello world tutorial. I think I am a bit beyond that.
Jul 1, 2012 at 7:38pm
I need each digit to be in a separate int.

So why aren't you doing that? Why are you making them a single six-digit int instead of making them separate ints?

You can make them separate ints when you create the file, by putting a line ending on each one.
1
2
3
4
outputFile << firstValue << endl;
outputFile << secondValue << endl;
outputFile << thirdValue << endl;
// and so on 


or you could put a space between each one.
1
2
3
4
outputFile << firstValue << " ";
outputFile << secondValue <<  " ";
outputFile << thirdValue << " ";
// and so on 


There are many other options, all of which make it so much easier to read them back in separately, instead of having to thrash around with inelegant number mashing. Alternatively, you can just read them in separately by reading in each digit one at a time as I showed you earlier; again, so much easier and simpler than number mashing.
Last edited on Jul 1, 2012 at 7:40pm
Jul 1, 2012 at 9:16pm
I need each digit to be in a separate int.

So why aren't you doing that? Why are you making them a single six-digit int instead of making them separate ints?


I am doing that. I realize now that I am just trying to do it the hard way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int a,b,c,d,e,f;
              
a = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << a;
b = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << b;
c = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << c;
d = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << d;
e = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << e;
f = tempnumbers % 10;
tempnumbers = tempnumbers / 10;
cout << f;


I realize now what you were saying when you said:

Even easier, however, would be for you to store each int on a separate line when you write them out. Then you could just read in each one as an int and you wouldn't have to do anything like this at all. You could just read them in.


In hindsight, I would have programmed my program to write and save the numbers on a separate line, so I would not have had to do all of this weird math stuff.

Thanks for all of your help.
Topic archived. No new replies allowed.