Reading file two ints at a time.

Does anyone know how I would go about reading a file two ints or chars at a time?

I already know how to read one char/int at a time, using the .get() method.

Say I have the number "63" in a file, to represent the hex form of a char. It's reading '6', and '3' separately.

Any help is appreciated.

EDIT: Also, if anyone knows how I would go about combining two or more int variables, it would be appreciated as well.
Example:

1
2
int i = 2, x = 6;
int a = i + x;


This returns the math result of 2 + 6, when I want it to return "26". Would using a char variable somewhere in there solve this problem?

Thanks.
Last edited on
Say I have the number "63" in a file, to represent the hex form of a char. It's reading '6', and '3' separately.
So you want to create a char with value 63? Read 63 as an integer and cast it to a char:
1
2
3
4
5
6
int n;
char c;
file >> n;
// n = 63
c = n;
// c = '?' 

This returns the math result of 2 + 6, when I want it to return "26"
a = i*10 + x = 26
There are myriad ways of doing what you ask. Is the "6" and "3" on a line by itself? Are the hex characters separated by spaces?

For your second question, you want to work with strings, not integers.
Let me be a little more clear.

I have a function that returns the hex value of a char, such as: int ( char x ).

I'm then writing the returned integer to a file, and than reading said integer from said file. Well, seeing as how most hex values consist of two digit numbers, such as '63', I'm not really getting what I want. When reading from said file, I'm getting '6', and then '3', using the file.get() method, which doesn't return the correct outcome when I convert back to ASCII.

So I really just need to know how to read two integers from a file, at a time. If it helps, I have all hex values in said file separated by a period, such as: 63.65.58.

As for the second question, I didn't really explain my whole situation, and I really do think I would have trouble doing so. So is there any way to combine the values of two integers in general, as your method is great Bazzy, but won't work under the conditions of my program.

I hope you understand, as I feel I'm still being a bit vague.

Thanks in advance.
Last edited on
Notice one thing: you are writing a formatted int to the file ( 63 ) which will result in two characters: '6' and '3'.
When reading a character from input, it will read only '6' or '3'.
You can read the number back to an int ( 63 ) and cast it to a char ( '?' ). You can't make a character out of two characters.

2nd Thing:
Try using stringstreams, something like this should work:
1
2
3
4
int n;
stringstream ss;
ss << 1 << 2; // these can be also variables
ss >> n; // n will be  = 12 
By "You can read the number back to an int", I'm guessing you mean I can read two different digits into one int variable, seeing as how you have '63' in parenthesis? This is what I am trying to accomplish. How would I go about doing this?

P.S. Please let me know if I misunderstand what you said.
If you have the ifstream, just read an integer as usual: file >> variable;
EDIT: I see what you're saying, but multiple hex values are in said file, each separated by a period.
Last edited on
I don't see the problem:
1
2
3
4
5
6
7
8
9
int i;
string s;
char t;
while ( file.good() )
{
    file >> hex >> i; // read number
    s += i;// add the character to the stream
    file >> t;// read the period
}
Thanks Bazzy, after a bit of tweaking of your snippet, I got my first problem fixed, but am still having trouble with my second problem.

I'm trying to combine two int values, that I get from a while loop. Here's a quick written example of what I'm trying to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example for Bazzy.
#include <iostream>
#include <fstream>
using namespace std;

int function ( int x ) {
       x = x + 5;
       return ( x );
}

int main ( ) {
	int a, b, c;
	ifstream file;
	file.open ( "/home/user/abc.txt" );
	if ( file.is_open ( ) ) {
		while ( ! file.eof ( ) ) {
			file >> a;
			b = function ( a );
			c = c + b; // Something along those lines.
		}
	}
	file.close ( );
        return 0;
}


As you can see, I'm trying to create an integer consisting of all the returned values of a, once it's passed through the function.
Didn't I show you how to do that?
http://www.cplusplus.com/forum/general/12131/#msg57899

I may not have understood well what you want to achieve, let's say that you want combine 12 and 34, the result should be 1234?
Topic archived. No new replies allowed.