473,807 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

input formatted file

Jun
hello,

I've a data file

========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========

first column represents the how many values in this line, and then
followed by values. I've seen some codes using

*************** *************** *
ifstream inf

while(inf >data1 >data2)
*************** *************** *
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.
Jun
Nov 24 '07 #1
10 1636
Hi,

Not tested, but my guess would be something like:

#include <vector>
#include <iostream>
using namespace std;

//........
try
{
unsigned long Cnt = 0;
while( inf >Cnt )
{
vector<unsigned longData;
for( unsigned long CntData = 0; CntData < Cnt; ++CntData )
{
unsigned long Item = 0;
if( !( inf >Item ) )
{
throw "Unexpecete d end of file";
}
Data.push_back( Item );
}
// Do something with data
}
}
catch( const char *Error )
{
cerr << Error << endl;
}
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Jun" <ju*****@gmail. comwrote in message
news:81******** *************** ***********@y5g 2000hsf.googleg roups.com...
hello,

I've a data file

========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========

first column represents the how many values in this line, and then
followed by values. I've seen some codes using

*************** *************** *
ifstream inf

while(inf >data1 >data2)
*************** *************** *
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.
Jun

Nov 24 '07 #2
"Jun" <ju*****@gmail. comwrote in message
news:81******** *************** ***********@y5g 2000hsf.googleg roups.com...
hello,

I've a data file

========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========

first column represents the how many values in this line, and then
followed by values. I've seen some codes using

*************** *************** *
ifstream inf

while(inf >data1 >data2)
*************** *************** *
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.
One thing I would do is use std::getline( inf, somestring );
then put it into a stringstream.
std::stringstre am Data( somestring );
At this point you can check if the number of values are correct.

int Count;
if ( Data >Count )
{
int Value;
int ValueCount;
while ( Data >Value )
{
ValueCount++;
// dosomething with Value
}
if ( ValueCount != Count )
{
// throw error here. Number at beginning does not match
// number of values.
}
}

If you don't use a stringstream but just do
while ( inf >Number )
you can't be sure if you're reading data or the count. If you were supposed
to be reading 5 values, such as the lines
5 1 1 1 1
2 1 1

It would read the next lines 2 as a value, grab the next 1 as a count, then
read the next 1 as a value, if you understand.

but getline will only read one line, which you can validate the count if you
need to.
Nov 24 '07 #3
Jun
On Nov 24, 5:37 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Jun" <junh...@gmail. comwrote in message

news:81******** *************** ***********@y5g 2000hsf.googleg roups.com...
hello,
I've a data file
========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========
first column represents the how many values in this line, and then
followed by values. I've seen some codes using
*************** *************** *
ifstream inf
while(inf >data1 >data2)
*************** *************** *
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.

One thing I would do is use std::getline( inf, somestring );
then put it into a stringstream.
std::stringstre am Data( somestring );
At this point you can check if the number of values are correct.

int Count;
if ( Data >Count )
{
int Value;
int ValueCount;
while ( Data >Value )
{
ValueCount++;
// dosomething with Value
}
if ( ValueCount != Count )
{
// throw error here. Number at beginning does not match
// number of values.
}

}

If you don't use a stringstream but just do
while ( inf >Number )
you can't be sure if you're reading data or the count. If you were supposed
to be reading 5 values, such as the lines
5 1 1 1 1
2 1 1

It would read the next lines 2 as a value, grab the next 1 as a count, then
read the next 1 as a value, if you understand.

but getline will only read one line, which you can validate the count if you
need to.
the following code
=============== =
std::stringstre am Data( somestring );
=============== =

just doesn't work
Nov 24 '07 #4
"Jun" <ju*****@gmail. comwrote in message
news:02******** *************** ***********@x69 g2000hsx.google groups.com...
On Nov 24, 5:37 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
>"Jun" <junh...@gmail. comwrote in message

news:81******* *************** ************@y5 g2000hsf.google groups.com...
hello,
I've a data file
========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========
first column represents the how many values in this line, and then
followed by values. I've seen some codes using
*************** *************** *
ifstream inf
while(inf >data1 >data2)
*************** *************** *
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.

One thing I would do is use std::getline( inf, somestring );
then put it into a stringstream.
std::stringstr eam Data( somestring );
At this point you can check if the number of values are correct.

int Count;
if ( Data >Count )
{
int Value;
int ValueCount;
while ( Data >Value )
{
ValueCount++;
// dosomething with Value
}
if ( ValueCount != Count )
{
// throw error here. Number at beginning does not match
// number of values.
}

}

If you don't use a stringstream but just do
while ( inf >Number )
you can't be sure if you're reading data or the count. If you were
supposed
to be reading 5 values, such as the lines
5 1 1 1 1
2 1 1

It would read the next lines 2 as a value, grab the next 1 as a count,
then
read the next 1 as a value, if you understand.

but getline will only read one line, which you can validate the count if
you
need to.

the following code
=============== =
std::stringstre am Data( somestring );
=============== =

just doesn't work
std::string somestring("5 1 1 1 1");
std::stringstre am Data( somestring );

make sure somestring is a std::string
you could call it anything, "somestring " was just a foo name.

Make sure you
#include <string>
#include <sstream>
Nov 24 '07 #5
On Nov 24, 5:37 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Jun" <junh...@gmail. comwrote in message
news:81******** *************** ***********@y5g 2000hsf.googleg roups.com...
I've a data file
========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========
first column represents the how many values in this line, and then
followed by values. I've seen some codes using
*************** *************** *
ifstream inf
while(inf >data1 >data2)
*************** *************** *
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.
One thing I would do is use std::getline( inf, somestring );
then put it into a stringstream.
Why a stringstream, and not an istringstream? You don't want to
write to it?
std::stringstre am Data( somestring );
At this point you can check if the number of values are correct.
int Count;
if ( Data >Count )
{
int Value;
int ValueCount;
while ( Data >Value )
{
ValueCount++;
// dosomething with Value
}
if ( ValueCount != Count )
{
// throw error here. Number at beginning does not match
// number of values.
}
}
Even easier:

std::istringstr eam s( line ) ;
size_t count ;
s >count ;
std::vector< int data((std::istr eam_iterator< int >( s )),
(std::istream_i terator< int >()) ) ;
if ( ! s ) {
// Some input error...
} else if ( data.size() != count ) {
// syntax error
}

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 24 '07 #6
"James Kanze" <ja*********@gm ail.comwrote in message
news:fb******** *************** ***********@j20 g2000hsi.google groups.com...
On Nov 24, 5:37 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Jun" <junh...@gmail. comwrote in message
news:81******** *************** ***********@y5g 2000hsf.googleg roups.com...
I've a data file
========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========
first column represents the how many values in this line, and then
followed by values. I've seen some codes using
*************** *************** *
ifstream inf
while(inf >data1 >data2)
*************** *************** *
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.
One thing I would do is use std::getline( inf, somestring );
then put it into a stringstream.
Why a stringstream, and not an istringstream? You don't want to
write to it?

----------

I don't use streams that often other than stringstream where I do tend to do
both input and output and the full blown stringstream is required. I'm not
100% sure of the advantages of using istringstream over stringstream though,
other than maybe some overhead costs.
std::stringstre am Data( somestring );
At this point you can check if the number of values are correct.
int Count;
if ( Data >Count )
{
int Value;
int ValueCount;
while ( Data >Value )
{
ValueCount++;
// dosomething with Value
}
if ( ValueCount != Count )
{
// throw error here. Number at beginning does not match
// number of values.
}
}
Even easier:

std::istringstr eam s( line ) ;
size_t count ;
s >count ;
std::vector< int data((std::istr eam_iterator< int >( s )),
(std::istream_i terator< int >()) ) ;
if ( ! s ) {
// Some input error...
} else if ( data.size() != count ) {
// syntax error
}
Nov 24 '07 #7
On Nov 24, 9:52 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"James Kanze" <james.ka...@gm ail.comwrote in message
[...]
Why a stringstream, and not an istringstream? You don't want to
write to it?
I don't use streams that often other than stringstream where I
do tend to do both input and output and the full blown
stringstream is required. I'm not 100% sure of the advantages
of using istringstream over stringstream though, other than
maybe some overhead costs.
Two important benefits: it expresses your intent better, and if
you slip up, and type a >instead of a <<, the compiler will
complain. (The < and the keys are adjacent on a US keyboard,
so if your right hand is slightly out of position, it's a likely
typo.)

It's interesting that you usually use both directions, however.
I've never had a case where I've needed a bi-directional
iostream, of any type. I can imagine them with fstream,
however, at least if you open the fstream in binary mode. I
can't even image a case where you would want one for a
stringstream, however. Are you using them as some sort of
temporary file? If so, why---what's the advantage compared to
simply saving the raw data in its internal format?

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 25 '07 #8

"James Kanze" <ja*********@gm ail.comwrote in message
news:54******** *************** ***********@n20 g2000hsh.google groups.com...
On Nov 24, 9:52 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"James Kanze" <james.ka...@gm ail.comwrote in message
[...]
Why a stringstream, and not an istringstream? You don't want to
write to it?
I don't use streams that often other than stringstream where I
do tend to do both input and output and the full blown
stringstream is required. I'm not 100% sure of the advantages
of using istringstream over stringstream though, other than
maybe some overhead costs.
Two important benefits: it expresses your intent better, and if
you slip up, and type a >instead of a <<, the compiler will
complain. (The < and the keys are adjacent on a US keyboard,
so if your right hand is slightly out of position, it's a likely
typo.)

It's interesting that you usually use both directions, however.
I've never had a case where I've needed a bi-directional
iostream, of any type. I can imagine them with fstream,
however, at least if you open the fstream in binary mode. I
can't even image a case where you would want one for a
stringstream, however. Are you using them as some sort of
temporary file? If so, why---what's the advantage compared to
simply saving the raw data in its internal format?

-------------------------

template<typena me T, typename F T StrmConvert( const F from )
{
std::stringstre am temp;
temp << from;
T to = T();
temp >to;
return to;
}
Nov 26 '07 #9
On Nov 26, 4:56 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"James Kanze" <james.ka...@gm ail.comwrote in message
[...]
It's interesting that you usually use both directions, however.
I've never had a case where I've needed a bi-directional
iostream, of any type. I can imagine them with fstream,
however, at least if you open the fstream in binary mode. I
can't even image a case where you would want one for a
stringstream, however. Are you using them as some sort of
temporary file? If so, why---what's the advantage compared to
simply saving the raw data in its internal format?
template<typena me T, typename F T StrmConvert( const F from )
{
std::stringstre am temp;
temp << from;
T to = T();
temp >to;
return to;
}
That's boost::lexical_ cast. Which is probably what I'd use if I
ever needed it. I'll admit that I never saw the utility of
boost::lexical_ cast either. And I'm rather sceptical of using
iostream inserters and extractors for this, too.

Consider something like:

std::complex< double z( 1.0, 0.0 ) ;
double x = boost::lexical_ cast< double >( z ) ;

The result should obviously be 1.0. Your code will return 0.0,
and boost::lexical_ cast will raise some sort of exception.

IMHO, the example showcases both problems:

-- converting a type A to a type B makes no sense in
general---you have to define exactly what is meant, for each
case (What should std::complex< double >( 0.0, 1.0 )
return when converted to double?), and

-- whatever definition you use, the >and << won't necessarily
do the trick.

(I wonder if this isn't some sort of larger anti-pattern:
providing a generic solution for a problem which doesn't have a
generic solution.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 27 '07 #10

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

Similar topics

10
4427
by: nkp | last post by:
Sorry guys, basic questions..... 1)how do you prevent negative numeric input in a text box (-2345 etc) 2)how do detect /prevent input of LESS than 8 characters in a text box( where a password must be a minimum of 8 characters for example) Thank you.
1
4870
by: ron | last post by:
Hi, I'm still new at Python and have been away from programming for a number of years in general. I apologized in advance if this has been discussed extensively already. Is the input() function new? There doesn't seem to be very many examples of it's use. After a lot of searching I did find both the input() and raw_input() statement definitions. I don't understand the reasoning behind making
4
2291
by: TheDD | last post by:
Hello all, i'm having a problem with input. My aim is to read a pbm file wich looks like: $ head file.pbm P1 # Created by Paint Shop Pro 7 3510 2550 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4
7988
by: Intaek LIM | last post by:
Hello. I always used cin in C++ for standard input. But, with C, I must use scanf. Integers, chars are all fine with scanf. The problem is... I cannot read floating point value like 'double' with scanf. ----------------------- double d;
2
2420
by: dkk | last post by:
I am new to C programming. I need to read data from a formatted input text file (column-based), for example, "12345abcde678", I want to parse it into "123", "45", "ab", "cde", "678", and write them into Oracle database. I also need to do it in reverse, that is, read data from Orable database and write them into a file on column basis. I need to do this in ANSI C. Anybody can give some suggestions or sample code? Thanks.
11
3359
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is called that line of info does not exist anymore in the file. for example i have created a short example of where i am at, but I dont know how to alter what im doing so that i just read in a single line
116
4647
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however, when I run it, it stalls and does nothing. I'm wondering if there's something obvious that I'm missing. My code is below and any help would be appreciated. Thanks, Dave
5
3033
by: Icarus - iD_Ten_T helper | last post by:
First of all, my apologies if this should be in a php newsgroup and not here, but I thought this the best place to start. I want to parse the text from a <textareaform element but when I pass this in a POST request to the server and then output it using PHP, all formatting is gone. I was under the impresssion that the textbox would register any carriage returns or line breaks and send them in the final text for processing?
209
8892
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from stdin. C takes input in 2 ways: 1) as a character, etchar() 2) as a whole line, fgets()
0
9720
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10626
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10112
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.