String -> Int

Started by
13 comments, last by stealth 21 years, 10 months ago
Show us some of your file.
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Advertisement
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


Well, as I said before. Instead of putting the number into a string put it into an integer.

Your .txt file:
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:

Your .txt file:
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]
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:

1) read line into string
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]
Thanks Michael thats EXACTLY what I was after thanks alot I think I will attempt to do that , thanks alot


cheers


Stealth

This topic is closed to new replies.

Advertisement