473,395 Members | 1,756 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,395 software developers and data experts.

easy way to read numbers from file

I have a file with numbers (all integers)
123,14,5,2,141345,3,4545, and so on where there is a , (comma) between every
integer or the comma can be replaced with a newline.
How do I put all these integers into vector<int>?

The best idea I have so far is

while (c = getc(file))
if (c>='0' && c<='9'){
number=number*10 + c-'0';
}
else {
vektor.push_back(number);
number=0;
}

Is there any better way?

Sep 10 '05 #1
1 6944

"Gunnar G" <de****@comhem.se> wrote in message
news:zd*********************@newsc.telia.net...
I have a file with numbers (all integers)
123,14,5,2,141345,3,4545, and so on where there is a , (comma) between
every
integer or the comma can be replaced with a newline.
How do I put all these integers into vector<int>?

The best idea I have so far is

while (c = getc(file))
if (c>='0' && c<='9'){
number=number*10 + c-'0';
}
else {
vektor.push_back(number);
number=0;
}

Is there any better way?


#include <istream>
#include <sstream>
#include <string>
#include <vector>

std::istream& get_nums(std::istream& file, std::vector<int>& vec)
{
vec.clear();
std::string s;

while(std::getline(file, s))
{
std::istringstream iss(s);
while(std::getline(iss, s, ','))
{
std::istringstream iss(s);
int num(0);

if(iss >> num)
vec.push_back(num);
}
}
return file;
}

-Mike
Sep 10 '05 #2

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

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
1
by: Jonas | last post by:
I want the user to be able to write like http://www.google.com in one textBox and Google in an other textBox. Than I want the user to press a button when done. The text will then be past like...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
2
by: kak3012 | last post by:
Hi, I have a text file I will read it and write out binary. The file includes 256 coloums. I use while (infile.good()) { infile.getline (buffer,2200);
1
by: Susan Sherpi | last post by:
Hello, I want to read a line of decimal integers from standard input into an array, stopping when a newline is encountered, and I'd like the function to return the number of values read in. ...
19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
4
by: I_have_nothing | last post by:
Hi! Is there any easy way to printf an integer in a way like 1,234,567? I know "%d" can be usd to print it as 1234567. Any type field in format specification can do that? Or any easy way to do...
6
by: Alexander Hunziker | last post by:
Hello group, I have a program that reads data from a binary file. I know that at some known position in the file there are 12 4 bytes long floating point numbers. Here's how I read them now: ...
4
by: Miner Jeff | last post by:
I've written some code that reads a text file. I have a requirement that the text file also be in a format that's easy to read when it's printed. In a prior project, I had used some spreadsheet...
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
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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 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.