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 12 2713
"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
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.
I posted the above message on comp.lang.c ; I have no idea
how it ended up here as well.
Spiros Bousbouras 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.
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 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
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
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.
}
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
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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...
|
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
|
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));...
|
by: mhw |
last post by:
I don't found function!
ThanksŁĄ
|
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...
|
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....
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |