![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 640
Rep Power: 4
![]() |
Read char as number value
say for example I have a file called key.txt
1234567890 and I wish to read from this file, digit by digit. with in being the ifstream object, I would write: char temp; in.get(temp) Now, I want this character to be converted to an int of the same value. so for Example, if I read the '4' character, I want to convert it into a int (4). the problem is that if I typecast it by doing this: (int)temp; I get the ASCII value, rather then the numerical value. How would I accomplish what I want? Thanks ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 550
Rep Power: 4
![]() |
Does C++ use or have an equivilant of the atoi() function?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
why can't c++ just use the atoi function?...(uh, it can...it's C++, not B++ or R++, hell.)
out of ignorance, let me suggest something harder than it should be... if you're reading only ONE character at a time, then you only have to worry about the int values of 0-9. just use a switch to assign the correct integer value to the ascii value. ten cases, trivial problem. i'm sure there's a better way.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
I'm with using atoi() ... no need to rewrite the wheel...
__________________
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Also, you can do something like
a=temp-'0';
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
i would use that instead of atoi, since this doesn't do more than it needs to do - so it is faster.
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
Is this for you Password Generator?
![]() I don't know how to deal with files yet, but i know that if u give a "char" variable a number value, it would be converted into an ASCII Character. EG. char x=65; cout << x << endl; This is What you will get on your screen... i hope i helped you with this...
__________________
From the bottom of the stone steps... ...i'm calling still. |
|
|
|
|
|
#9 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Oct 2005
Location: Indiana
Posts: 1
Rep Power: 0
![]() |
I believe that atoi() is for strings, not chars.
How about a simple subtraction... int val = int(n) - 48; Here's a quick sample... #include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { string filepath = "D:\\ints.txt"; ifstream in(filepath.c_str()); if(!in.is_open()) { cerr << "Error opening " << filepath; return -1; } char n; int via_subtract; const int ASCII_ZERO = 48; in.get(n); while(!in.eof()) { via_subtract = int(n) - ASCII_ZERO; cout << "Input = \'" << n << "\'\tvia_subtract = " << via_subtract << "\n"; in.get(n); } in.close(); return 0; } Hope that helps |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|