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.
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.
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
}
// Example for Bazzy.
#include <iostream>
#include <fstream>
usingnamespace 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.