473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

string->int

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

Jun 2 '06 #1
12 2712

"grubbymaster" <gr**********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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
Jun 2 '06 #2

grubbymaster wrote:
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.

Jun 2 '06 #3
I posted the above message on comp.lang.c ; I have no idea
how it ended up here as well.

Spiros Bousbouras

Jun 2 '06 #4
sp****@gmail.com wrote:
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.

Jun 2 '06 #5

grubbymaster wrote:
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-lit....html#faq-39.2

Jun 2 '06 #6

sp****@gmail.com a scris:
grubbymaster wrote:
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

Jun 2 '06 #7
In comp.lang.c++ sp****@gmail.com wrote:
grubbymaster wrote:
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.


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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 2 '06 #8
grubbymaster wrote:
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] =
.....


comp.lang.c removed from headers.

Other people have addressed the conversion issue (speaking personally,
I'd use a stringstream). So I'll address the two other problems.

Your read loop is incorrect, you will get an extra read upon EOF.
Search the FAQ. The idiom you want is:

while (LZW.getline(...))
{
}

In addition, you should probably use a std::string instead of a char
array.

e.g.

#include <string>
#include <fstream>

// ... redacted

std::string buffer;
ifstream LZW(sz_path);

while (std::getline(LZW, buffer))
{
// do something with buffer.
}
Jun 2 '06 #9
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
Jun 2 '06 #10
Uwe Thormann <uw*********@gmx.de> writes:
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++.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 2 '06 #11
Keith Thompson wrote:
Uwe Thormann <uw*********@gmx.de> writes:
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.


Me neither. knode quotes the original posting by default when replying. Uwe
must have switched that off explicitly somehow or cut the quoting away for
some reason.
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++.


That was probably due to knode. When replying to a cross-posting, it adds a
follow-up to the first group in the list. It's easy to overlook that.
Jun 3 '06 #12
Keith Thompson wrote:

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

Jun 5 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ames Andreas (MPA/DF) | last post by:
Hi all, is it possible (within a C extension) to create a python string (or integer or other type) from an existing C string *without* copying the string? TIA, andreas
7
by: ma740988 | last post by:
The string object value_f doesn't produce the right output. At issue, - I suspect - is the conversion from string to int with istringstream. An alternate approach? Thanks in advance #include...
9
by: ritchie | last post by:
Hello, Just wondering if anyone could help me with a basic C string-parsing question. I have an Integer date ie: YYYYMMDD. I can convert it into a string but I want to break it down to: Int...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
2
by: js | last post by:
Hello, I have two text boxes accepting ints and displaying their Sum. This code works: string ans; ans = Convert.ToString(Convert.ToInt32(textBoxX.Text) + Convert.ToInt32(textBoxY.Text));...
4
by: mhw | last post by:
I don't found function! ThanksŁĄ
3
by: NathanC | last post by:
Left('ironman',4) Result - 'iron' Is there anyway to excute this task in VB.NET? Currently, I am determing the value of the 4th character, splitting on that, then grabbing the value of the...
1
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, can someone please tell me if there's a big difference among the following: i want to store a string and an int in session. is it better to store it like: 1. delimited string (i.e....
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.