#### Archived

This topic is now archived and is closed to further replies.

# String -> Int

This topic is 5675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was wondering u see I am reading strings froma file , I was wondering how do I convert the string to an int if the string represents an int?? I know its hard ot understand, but I think this is kinda easy OI just dunno how to do it say I had a string "100" how do I turn that into an INT of 100?? thanks to anyone who can help Cheers Stealth

##### Share on other sites
int n;ifstream file("file.txt");file >> n;

[edited by - Ragadast on June 4, 2002 11:50:07 PM]

##### Share on other sites
i havent used them in a long time, so youll have to look up the syntax, but the functions itoa, atoi, ftoa, and atof will do what you want.

##### Share on other sites

      #include <string>#include <sstream>int n;std::string str = "100";std::istringstream is( str );is >> n;

  #include <string>#include <boost/lexical_cast.hpp>int n;std::string str = "100";n = boost::lexical_cast<int>( str );str = boost::lexical_cast<std::string>( 10000 );

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

[edited by - Fruny on June 4, 2002 11:48:02 PM]

[edited by - Fruny on June 4, 2002 11:48:16 PM]

##### Share on other sites
quote:
"int n;
ifstream file("file.txt");
file >> n;

I don''t think that will work. Characters in text files are stored with ASCII encoding or something. For example, I believe you will get a 48 if you read in a zero from a text file. Atof, atoi, and that family, however, will do the job; I recently had to use them to convert input from a string to an int for a calculator program I had to write at school.

##### Share on other sites
Use
int atoi( const char *string );

So, if you got string like:
char number10[10] = "10";

then your number as integer is:
int num = atoi(number10);

Cheers.

" Do we need us? "

##### Share on other sites
If you're using C, you can do:

      char * IntReadIn = "4242";int Converted;sscanf(IntReadIn, "%d", &Converted);

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 4, 2002 12:50:56 AM]

[edited by - Chem0sh on June 4, 2002 12:51:14 AM]

##### Share on other sites
You may also consider storing your numbers in binary. It''s much more efficient than using ASCII text, although you won''t be able to edit them in a normal text editor. Look into the "read" and "write" methods of ifstream and ofstream. Make sure you open the file in ios::binary mode, too.

##### Share on other sites
Thanks for the help but I am even more confused

what I am actually doing is reading the string from a fiel which means I CANT store as anything else but that , the little project I am working on is something like an interpreter , so therefore it has to take strings from a file , strings which will reperesent ints , so something like

string MyString;
MyString = "100";

I have to change this to an int to do something like:

string MyString;
MyString = "100";

MyInt = MyString(converted to int of course);

NewInt = 300 - MyInt;

something like that

, this stuff is starting to get me confused lol , I ACTUALLY want to take the Number which WILL be a number represented by a string , so it HAS to be stored as a string but it WILL be a number , so can some one plz explain ina little more of an understandable way?

thanks to all those who helped and I''ll continue to try and understand the help I have been given

cheers

Stealth

##### Share on other sites
Hm, even based on this clarification, it still sounds like what you need is the atoi faimily. For example...

(Psuedo code)

- Read value you from file (it will be a string)
- Convert to int use atoi()
- Do necessary operations using ints
- Write to file (it will become a string again)

Is this what you''re trying to do?

##### Share on other sites
Show us some of your file.

##### Share on other sites
heres the code

//----------------------------------------------------

#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;

string WB_TITLE;
int WB_WIDTH;
int WB_HEIGHT;

string GET_WB_TITLE()
{
ifstream in("title.txt"); // Open for reading
ofstream out("titlecopy.txt"); // Open for writing
string title;

getline(in, title); // Discards newline char
out << title << "\n"; // ... must add it back
WB_TITLE = title;
return WB_TITLE;
}

int GET_WB_WIDTH()
{
ifstream in("width.txt"); // Open for reading
ofstream out("widthcopy.txt"); // Open for writing
string width;

getline(in, width); // Discards newline char
out << width << "\n"; // ... must add it back
return 0;
}

int GET_WB_HEIGHT()
{
ifstream in("height.txt"); // Open for reading
ofstream out("heightcopy.txt"); // Open for writing
string height;

getline(in, height); // Discards newline char
out << height << "\n"; // ... must add it back
return 0;
}

int main()
{
GET_WB_TITLE();
GET_WB_WIDTH();
GET_WB_HEIGHT();

system("PAUSE");
return 0;
}

//----------------------------------------------------------

now u see what I''m tryna do
the WB_WIDTH & WB_HEIGHT will be intergers used later on , but first they have to be read in as a string from a file , unless there is a way to read an int straight from a file????

anyways I hope this helps

Cheers

Stealth

##### Share on other sites
Well, as I said before. Instead of putting the number into a string put it into an integer.

3231

To retrieve that number:
int n;ifstream in("yourfile.txt");in >> n;in.close();   

Now, if you have a string before the number. For example:

WIDTH 400

You do this:
string word;int n;ifstream in("yourfile.txt");in >> word >> n;in.close();   `

Remember to include the headers files fstream and string if you are using it. I hope that helps

[edited by - Ragadast on June 5, 2002 10:04:39 PM]

##### Share on other sites
I'm not sure if everyone is getting what he's saying. He's reading lines of a file into strings, and wants to check if an integer just happens to be in the line he just read.

I think something along the lines of this might work:

2) loop through string and perform an isdigit() on each char
3) if isdigit() returns true, then sscanf() the string you just read in for your int (or float, whatever).

It'd only be a little more complicated if there'd also be floats in there. I guess you could just assume that it would be a float number and then check if there's a decimal other than 0. if not, convert it to an int, etc.

-Michael

[edited by - Michael Grazier on June 5, 2002 10:20:37 PM]

##### Share on other sites
Thanks Michael thats EXACTLY what I was after thanks alot I think I will attempt to do that , thanks alot

cheers

Stealth