473,324 Members | 2,268 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,324 software developers and data experts.

How to change delineator in iostream?

Let's say I have something like this:

ifstream inFile( buffer, ios::in );

while ( inFile >> name >> value ) { // read lines
cout << "Name: " << name << " Value: " << value;
}

The file reads one word at a time, which appears to be delineated by
whitespace or a newline. How do I change the delineator, for example,
using '=' instead of ' '?
Jul 22 '05 #1
3 2651
In article <40********@buckaroo.cs.rit.edu>,
Jennica Humphrey <jj*****@STOPSPAM.rit.edu> wrote:

while ( inFile >> name >> value ) { // read lines
cout << "Name: " << name << " Value: " << value;
}

The file reads one word at a time, which appears to be delineated by
whitespace or a newline. How do I change the delineator, for example,
using '=' instead of ' '?


Use getline() instead of the >> operator. It allows you to specify a
terminator character in place of the default '\n'. For example, if you
have three tab-delimited fields per line (and each line is terminated by a
newline):

std::string field1, field2, field3;
while (getline (infile, field1, '\t') && getline (inFile, field2, '\t')
&& getline (inFile, field3))
{
cout << field1 << '\t' << field2 << '\t' << field3 << endl;
}

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #2


Jennica Humphrey wrote:
Let's say I have something like this:

ifstream inFile( buffer, ios::in );

while ( inFile >> name >> value ) { // read lines
cout << "Name: " << name << " Value: " << value;
}

The file reads one word at a time, which appears to be delineated by
whitespace or a newline. How do I change the delineator, for example,
using '=' instead of ' '?

A co-worker asked me this the other day, as it's apparently really easy
to do in Java..

I can't tell you exactly how, but I think you can do this by
constructing a locale with a ctype for which isspace returns true on '='
(and false on ' ', '\t', etc..) Then "imbue" whatever stream you want
to get input from with your new locale..

You could build a nice little stream manipulator which does this all for
you.

Jason

Jul 22 '05 #3

Thank you I did:

while ( inFile.getline( name, 40, '=' ) && inFile.getline( value, 50 ) )

and that did the trick.

Jon Bell wrote:
In article <40********@buckaroo.cs.rit.edu>,
Jennica Humphrey <jj*****@STOPSPAM.rit.edu> wrote:

while ( inFile >> name >> value ) { // read lines
cout << "Name: " << name << " Value: " << value;
}

The file reads one word at a time, which appears to be delineated by
whitespace or a newline. How do I change the delineator, for example,
using '=' instead of ' '?

Use getline() instead of the >> operator. It allows you to specify a
terminator character in place of the default '\n'. For example, if you
have three tab-delimited fields per line (and each line is terminated by a
newline):

std::string field1, field2, field3;
while (getline (infile, field1, '\t') && getline (inFile, field2, '\t')
&& getline (inFile, field3))
{
cout << field1 << '\t' << field2 << '\t' << field3 << endl;
}

Jul 22 '05 #4

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

Similar topics

10
by: John Tiger | last post by:
Can anybody have idea about the difference between #include <iostream.h> and #include <iostream>. Is later one valid statement on any compiler. I tried compiling on MSVC second statement give...
19
by: ernst.stiegler | last post by:
Hello, I just want to read a whole line from a console input. What I don't understand is that I always have to press ENTER twice in order to see the line I've entered. Here's my code : ...
1
by: Pradyot Dhulipala | last post by:
Hi, I am using ns2.26 with the Makefile using C++ compiler version c++ (GCC) 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk). When I use iostream.h in a simple program and compile with c++ everything works...
2
by: humble04 | last post by:
Hi, I am compiling a collection of C++ code. Most of them are using the new format #include <iostream>. I think all of them because I failed at finding out which header file uses the old format ...
5
by: Charles F McDevitt | last post by:
I'm converting some old programs that use old iostreams. In one program, the program is using cout to output to the stdout stream. Part way through, the program wants to put some binary data out,...
1
by: Bill Sun | last post by:
Hi, I just want to using a console application in the .net enviroment. Before the main(), I using the #include "iostream.h" but the .net tell me:...
2
by: Brett | last post by:
Hi, I'm compiling an old project under the 'new' visual studio 7.1.3088. I changed the line: #include <iostream.h> to #include <iostream> and now I get a stack of errors, some of which...
2
by: NewToCPP | last post by:
I am having some trouble including "iostream" "fstream" to my code. I tried the following ways and base times it did not work. What is the problem? test.cc: ======= ..... #include...
13
by: Sakula | last post by:
#define MAX 20 /* includes */ #include<iostream.h> #include<stdio.h> #include<malloc.h> #include<string.h>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.