string->int

G

grubbymaster

hello

i'm extracting from a file numbers that are stored on each line.
this is the only way i know to extract each line at a time from a file
, using <fstream> <ostream> :

char buffer[32];

ifstream LZW(sz_path);

while(!LZW.eof())
{
memset(buffer, 0, sizeof(buffer));

LZW.getline(buffer, sizeof(buffer)-1);
}

// buffer holds for example : "123" and i want to convert it to int x =
123 so that i can work with it later , like for example my_array[123] =
......

can anybody provide help in this aspect.

thank you,
alex
 
A

Allan M. Bruce

grubbymaster said:
hello

i'm extracting from a file numbers that are stored on each line.
this is the only way i know to extract each line at a time from a file
, using <fstream> <ostream> :

char buffer[32];

ifstream LZW(sz_path);

while(!LZW.eof())
{
memset(buffer, 0, sizeof(buffer));

LZW.getline(buffer, sizeof(buffer)-1);
}

// buffer holds for example : "123" and i want to convert it to int x =
123 so that i can work with it later , like for example my_array[123] =
.....

can anybody provide help in this aspect.

thank you,
alex

have a look at sscanf()
Allan
 
S

santosh

grubbymaster said:
hello

i'm extracting from a file numbers that are stored on each line.
this is the only way i know to extract each line at a time from a file
, using <fstream> <ostream> :

okay this *looks* like C++. This group deals only with C. Unless you
have questions on C, please post any further replies to comp.lang.c++
alone.
char buffer[32];

ifstream LZW(sz_path);

while(!LZW.eof())
{
memset(buffer, 0, sizeof(buffer));

LZW.getline(buffer, sizeof(buffer)-1);
}

// buffer holds for example : "123" and i want to convert it to int x =
123 so that i can work with it later , like for example my_array[123] =
.....

can anybody provide help in this aspect.

The people in comp.lang.c++ will tell you the method in C++, but in C,
the sscanf() function can, among others, do the job.
 
S

spibou

grubbymaster said:
hello

i'm extracting from a file numbers that are stored on each line.
this is the only way i know to extract each line at a time from a file
, using <fstream> <ostream> :

fstream and ostream are not C.
// buffer holds for example : "123" and i want to convert it to int x =
123 so that i can work with it later , like for example my_array[123] =

If I understand correctly what you want , atoi() is what you're looking
for.
 
S

spibou

I posted the above message on comp.lang.c ; I have no idea
how it ended up here as well.

Spiros Bousbouras
 
S

santosh

I posted the above message on comp.lang.c ; I have no idea
how it ended up here as well.

Spiros Bousbouras

Because you included both the groups in your header. Delete
comp.lang.c++.

Further please quote context in your posts. Not all Usenet readers can
easily access the previous posts.
 
S

santosh

grubbymaster wrote:
.... snip ...
// buffer holds for example : "123" and i want to convert it to int x =
123 so that i can work with it later , like for example my_array[123] =

If I understand correctly what you want , atoi() is what you're looking for.

A better way is to use sscanf() or strtol(). atoi() does not indicate
failure.
 
W

wittempj

grubbymaster said:
hello

i'm extracting from a file numbers that are stored on each line.
this is the only way i know to extract each line at a time from a file
, using <fstream> <ostream> :

char buffer[32];

ifstream LZW(sz_path);

while(!LZW.eof())
{
memset(buffer, 0, sizeof(buffer));

LZW.getline(buffer, sizeof(buffer)-1);
}

// buffer holds for example : "123" and i want to convert it to int x =
123 so that i can work with it later , like for example my_array[123] =
.....

can anybody provide help in this aspect.

thank you,
alex

The faq gives a direction on how to approach this, see
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
 
G

grubbymaster

(e-mail address removed) a scris:
grubbymaster said:
hello

i'm extracting from a file numbers that are stored on each line.
this is the only way i know to extract each line at a time from a file
, using <fstream> <ostream> :

fstream and ostream are not C.
// buffer holds for example : "123" and i want to convert it to int x =
123 so that i can work with it later , like for example my_array[123] =

If I understand correctly what you want , atoi() is what you're looking
for.

10x alot. can believe it was that simple.i've searched google before
posting and couldn't find what i was looking for.

bye
 
M

Marcus Kwok

In said:
fstream and ostream are not C.

But they are C++. The OP crossposted to both clc and clc++, which is
usually not a good idea, since they are different languages.
// buffer holds for example : "123" and i want to convert it to int x =
123 so that i can work with it later , like for example my_array[123] =

If I understand correctly what you want , atoi() is what you're looking
for.

I would recommend strtod(), because if you use atoi() you have no way of
knowing whether the conversion was successful.

Personally I would use stringstreams, but those are not C so I will
refrain from posting that solution in this message.
 
S

spibou

Marcus said:
I would recommend strtod(), because if you use atoi() you have no way of
knowing whether the conversion was successful.

He said he wanted int ; strtod() returns double.

Spiros Bousbouras
 
U

Uwe Thormann

hello,

#include<fstream>
#include<sstream>
#include<string>
#include<iostream>

int main(){
int a;
std::ifstream LZW("123.txt");
std::string buffer;

while(LZW >> buffer){
std::stringstream streambuffer(buffer);
streambuffer >> a;
std::cout << 2*a << std::endl; // for example
}
}

Uwe
 
K

Keith Thompson

Uwe Thormann said:
hello,

#include<fstream>
#include<sstream>
#include<string>
#include<iostream>

int main(){
int a;
std::ifstream LZW("123.txt");
std::string buffer;

while(LZW >> buffer){
std::stringstream streambuffer(buffer);
streambuffer >> a;
std::cout << 2*a << std::endl; // for example
}
}

First, without context, it's hard to tell what you're talking about.
I know that groups.google.com has problems with this, and the
workaround has been posted in comp.lang.c hundreds of times, but
you're not posting through Google, so I don't know what to tell you.

Second, you posted a C++ program and directed followups to
comp.lang.c. Was that a typo? I've directed followups to
comp.lang.c++.
 
A

ais523

Keith Thompson wrote:

First, without context, it's hard to tell what you're talking about.
I know that groups.google.com has problems with this, and the
workaround has been posted in comp.lang.c hundreds of times, but
you're not posting through Google, so I don't know what to tell you.

<snip>

In fact, the Google interface has now been fixed in this regard. This
message was written via the 'Reply' link at the bottom of your message,
and the quoting was done automatically; I even had a chance to snip it.


(I missed the clc++ followup the first time I posted this; this is a
repost to clc, where I meant it to go.)
 
K

Keith Thompson

ais523 said:
Keith Thompson wrote:



<snip>

In fact, the Google interface has now been fixed in this regard. This
message was written via the 'Reply' link at the bottom of your message,
and the quoting was done automatically; I even had a chance to snip it.

I just tried it, and this appears to be correct. However, I've seen
this before, and it wasn't actually fixed then (at least not
permanently). If it's been fixed, we'll see if it stays that way.
 
C

CBFalconer

ais523 said:
Keith Thompson wrote:



<snip>

In fact, the Google interface has now been fixed in this regard. This
message was written via the 'Reply' link at the bottom of your message,
and the quoting was done automatically; I even had a chance to snip it.

(I missed the clc++ followup the first time I posted this; this is a
repost to clc, where I meant it to go.)

I wonder if Google has fixed it everywhere. You may remember that
for a time after introducing the ugly interface their old (and
saner) interface was available on various national access points.
I am still seeing various contextless replies originating on
Google, but ignoring them.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." -- Hermann Goering.
 
N

Nelu

I just tried it, and this appears to be correct. However, I've seen
this before, and it wasn't actually fixed then (at least not
permanently). If it's been fixed, we'll see if it stays that way.
Maybe they've got it right this time. Too late, though, I'm better off
with slrn :).
 
J

Jordan Abel

2006-06-05 said:
Maybe they've got it right this time. Too late, though, I'm better off
with slrn :).

Except for the lack of utf-8 support - anyone know a good text-based
newsreader with utf-8 support?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top