Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 18th, 2005, 10:25 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 640
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Oct 18th, 2005, 10:34 PM   #2
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 550
Rep Power: 4 Benoit is on a distinguished road
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
Benoit is offline   Reply With Quote
Old Oct 18th, 2005, 11:50 PM   #3
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Oct 19th, 2005, 6:17 AM   #4
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
...a better way
use a map.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Oct 19th, 2005, 6:42 AM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
I'm with using atoi() ... no need to rewrite the wheel...
__________________

tempest is offline   Reply With Quote
Old Oct 19th, 2005, 6:44 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Also, you can do something like
a=temp-'0';
to get what you want.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Oct 19th, 2005, 7:06 AM   #7
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
i would use that instead of atoi, since this doesn't do more than it needs to do - so it is faster.
Polyphemus_ is offline   Reply With Quote
Old Oct 19th, 2005, 7:19 AM   #8
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
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.
darkone916 is offline   Reply With Quote
Old Oct 19th, 2005, 7:34 AM   #9
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by darkone916
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.
char variables contain always numbers. When you give it a value like 'A', the compiler converts it to a number. cout also doesn't convert it. The place where the characters are viewed, will the number be converted. When working in ungraphic mode, the video card converts it.
Polyphemus_ is offline   Reply With Quote
Old Oct 19th, 2005, 7:56 AM   #10
jostrom
Newbie
 
jostrom's Avatar
 
Join Date: Oct 2005
Location: Indiana
Posts: 1
Rep Power: 0 jostrom is on a distinguished road
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
__________________
Joshua Ostrom
Software Developer
jostrom is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC