Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Read char as number value (http://www.programmingforums.org/showthread.php?t=6489)

Jessehk Oct 18th, 2005 11:25 PM

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 :)

Benoit Oct 18th, 2005 11:34 PM

Does C++ use or have an equivilant of the atoi() function?

bl00dninja Oct 19th, 2005 12:50 AM

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.

InfoGeek Oct 19th, 2005 7:17 AM

Quote:

...a better way
use a map.

tempest Oct 19th, 2005 7:42 AM

I'm with using atoi() ... no need to rewrite the wheel...

InfoGeek Oct 19th, 2005 7:44 AM

Also, you can do something like
:

a=temp-'0';
to get what you want.

Polyphemus_ Oct 19th, 2005 8:06 AM

i would use that instead of atoi, since this doesn't do more than it needs to do - so it is faster.

darkone916 Oct 19th, 2005 8:19 AM

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...

:

A

i hope i helped you with this...

Polyphemus_ Oct 19th, 2005 8:34 AM

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.

jostrom Oct 19th, 2005 8:56 AM

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


All times are GMT -5. The time now is 8:39 AM.

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