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

seeking within ostringstream's

What is a stringsteam supposed to do when you seek past the end of
existing buffer. I can seek past the end of a file stream (my
implementation fills the space will nulls but I cannot find if this is
guaranteed either)

Below code fails for a stringstream. Any good ways of dealing with
this.

Reason for doing this is that I am modify old C code for screen output
that positions fields based on a row. I wanted to use a stringstream
and just seek to the correct place.

I have tried basically filling the stream with X spaces first. But
then I am never sure there is enough space.

Any help appreciated.

Adrian

#include <iostream>
#include <sstream>
#include <fstream>

int main(int argc, char *argv[])
{
std::ofstream out("test.txt");

out.seekp(500);
out << "some text\n";

std::ostringstream out2;

out2.seekp(500);
out2 << "some text\n";

if(!out2)
{
std::cout << "bad out2\n";
}
return 0;
}

Sep 13 '07 #1
7 3073

Adrian <nn**@bluedreamer.comwrote in message...
What is a stringsteam supposed to do when you seek past the end of
existing buffer. I can seek past the end of a file stream (my
implementation fills the space will nulls but I cannot find if this is
guaranteed either)

Below code fails for a stringstream. Any good ways of dealing with
this.

Reason for doing this is that I am modify old C code for screen output
that positions fields based on a row. I wanted to use a stringstream
and just seek to the correct place.

I have tried basically filling the stream with X spaces first. But
then I am never sure there is enough space.

Any help appreciated.
Adrian

#include <iostream>
#include <sstream>
#include <fstream>

int main(int argc, char *argv[]){
std::ofstream out("test.txt");

out.seekp(500);
out << "some text\n";

std::ostringstream out2;

out2.seekp(500);
if( not out2 ){
std::cout << "out2 in failure\n";
}
else{
std::cout << "out2 position=" <<out2.tellp()<<'\n';
}
out2 << "some text\n";

if(!out2)
{
std::cout << "bad out2\n";
}
return 0;
}
Your result?

--
Bob R
POVrookie
Sep 13 '07 #2
On Sep 13, 12:40 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
Below code fails for a stringstream. Any good ways of dealing with
this.
Your result?
The stringstream is not in a good state. I tried it with exceptions on
and I get
"basic_ios::clear" from what()
Sep 13 '07 #3

Adrian <nn**@bluedreamer.comwrote in message...
On Sep 13, 12:40 pm, "BobR" wrote:
Below code fails for a stringstream. Any good ways of dealing with
this.
Your result?

The stringstream is not in a good state. I tried it with exceptions on
and I get
"basic_ios::clear" from what()
I think a safer way to move to a position might be:

std::ostringstream out2;
// ....

size_t pos( out2.tellp() );
std::string Space( ( 500 - pos ), ' ' );

// out2.seekp(500);
out2<<Space;

// ....

--
Bob R
POVrookie
Sep 13 '07 #4
On Sep 13, 7:05 pm, Adrian <n...@bluedreamer.comwrote:
What is a stringsteam supposed to do when you seek past the end of
existing buffer. I can seek past the end of a file stream (my
implementation fills the space will nulls but I cannot find if this is
guaranteed either)
I can't find anything really specifying it either, but I suspect
that it's undefined behavior (or unspecified). It also depends
on whether you are using the two argument form of seek, or the
one argument form. (With the one argument form, it's actually
impossible to obtain a position which you haven't already
visited without involving undefined or unspecified behavior.
With the possible exception of start of file.)

Logically, one might say that a good implementation would
generate an error on an attempt to seek beyond end of file, but
in practice, this would have significant run-time cost on some
systems.
Below code fails for a stringstream. Any good ways of dealing with
this.
Reason for doing this is that I am modify old C code for
screen output that positions fields based on a row. I wanted
to use a stringstream and just seek to the correct place.
I have tried basically filling the stream with X spaces first.
But then I am never sure there is enough space.
#include <iostream>
#include <sstream>
#include <fstream>
int main(int argc, char *argv[])
{
std::ofstream out("test.txt");
out.seekp(500);
The above line is not even guaranteed to compile, much less do
anything useful.
out << "some text\n";
std::ostringstream out2;
out2.seekp(500);
And the same holds true here.
out2 << "some text\n";
if(!out2)
{
std::cout << "bad out2\n";
}
return 0;
}
I'm not too sure what you're doing, but it sounds like what you
want is something like std::string or std::vector<char>, using
resize( ' ' ) as needed.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 14 '07 #5
On Sep 14, 3:34 am, James Kanze <james.ka...@gmail.comwrote:
I'm not too sure what you're doing, but it sounds like what you
want is something like std::string or std::vector<char>, using
resize( ' ' ) as needed.
I am trying to clean up and rewrite some old code that is creating
rows for screen display that uses cursor position and printf type
args.

Thing is I can never tell exactly how long the row could be. And the
version of snprintf on this platform returns -1 if printed string is
longer then buffer rather then what should be then length.

I was hoping with seeking in a stringstream I could keep almost the
same arguments

Example of old stuff below:
pn1(0, row, "%-10s", HemName_of(obj));
pn1(11, row, "%-2s",
Area_sh_nms[Mt_AreaFromCnum(Mt_Cnum_of(obj))]);
pn1(14, row, "%-8d", Mt_Cnum_of(obj));
pn1(23, row, "%-6s", Mt_ObjTypeName_of(obj));
pn1(30, row, "%-7s", Mt_CmdtyName_of(obj));
if (Mt_ObjType_of(obj) == EQUITY_TYPE)
pn1(38, row, "%-25s", Mt_Name_of(obj));
else {
strcpy(XSymb, Mt_Name_of(obj));
if (strlen(XSymb) 14) {
XSymb[14] = '*';
XSymb[15] = '\0';
}
pn1(38, row, "%-15s", XSymb);
}
if (Mt_ObjType_of(obj) == FUTURE_TYPE)
{
pn1(54, row, "%-.6ld", Mt_dtoymd(Mt_ExpDate_of(obj)));
pn1(62, row, "%-6s",
HemName_of(Mt_FindByLdOptIndx(Mt_RelLdOpt_of(obj)) ));
}
else if (Mt_ObjType_of(obj) == BOND_TYPE)
{
pn1(54, row, "%-.6ld", Mt_dtoymd(Mt_ExpDate_of(obj)));
pn1(62, row, "%-6s", Mt_SymAlias_of(obj));
}
if (((Mt_ObjType_of(obj) == FX_TYPE) ||
(Mt_ObjType_of(obj) == CASH_VOL_TYPE))
&& (Mt_HeFlags_of(Mt_Cmdty_of(obj)) & CASH_SCALE_MSK))
{
pn1(69, row, "%-7s", Display_scale_of(Mt_CshScale_of(obj)));
}
else
pn1(69, row, "%-7s", Display_scale_of(Mt_Scale_of(obj)));
pn1(77, row, "%-3s", TransExchangeName(obj));
if (Mt_ObjType_of(obj) == BOND_TYPE)
{
if ((Mt_HeFlags_of(obj) & ALIAS_MSK) != 0)
pn1(76, row, "%c", 'A');
}
break;

Sep 14 '07 #6

Adrian <nn**@bluedreamer.comwrote in message...
>
I am trying to clean up and rewrite some old code that is creating
rows for screen display that uses cursor position and printf type
args.

Thing is I can never tell exactly how long the row could be. And the
version of snprintf on this platform returns -1 if printed string is
longer then buffer rather then what should be then length.
size_t rows(25);
size_t cols(80);
std::vector<std::stringvScrn( rows, std::string( cols, ' ' ) );

So, now you have an 25x80 "screen". But, now you can change it's size at
will.

// add a row
vScrn.push_back( std::string( cols, ' ' ) );

// make all cols 90
for( size_t i(0); i < vScrn.size(); ++i ){
vScrn.at(i).append( 10, ' ' ); // add 10 spaces
} // for(i)

// make it 20x50
vScrn.resize( 20 );
for( size_t i(0); i < vScrn.size(); ++i ){
vScrn.at(i).resize( 50 );
} // for(i)

// Change the char at row 3, col 22, to "A":
vScrn.at(2).at( 21 ) = 'A';

// send the whole thing to cout:
std::copy( vScrn.begin(), vScrn.end(),
std::ostream_iterator<std::string>( std::cout, "\n" ) );

Etc..

You could put it all in one std::string, but then you'd need to keep track
of the new-line chars ('\n').
std::string( " line one.\n line two.\n line three.\n ....." );

I much prefer the vector of strings. Use the power of the standard C++
library.
>
I was hoping with seeking in a stringstream I could keep almost the
same arguments
For a learning excersize, go ahead. Otherwise, re-design it from the ground
up. I'm still fighting an C_to_C++ project I started years ago, a real mess.
(if only I knew then what I know now! <G>).
>
Example of old stuff below:
pn1(0, row, "%-10s", HemName_of(obj));
Without knowing what declaration of 'pn1' is, it's hard to give you an
example.

Think it over, and come back here if/when you need help.

--
Bob R
POVrookie
Sep 14 '07 #7
Adrian wrote:
On Sep 14, 3:34 am, James Kanze <james.ka...@gmail.comwrote:
I'm not too sure what you're doing, but it sounds like what you
want is something like std::string or std::vector<char>, using
resize( ' ' ) as needed.
I am trying to clean up and rewrite some old code that is creating
rows for screen display that uses cursor position and printf type
args.
But that doesn't really tell me much. The obvious way to
represent rows in a screen display would be something like:
typedef std::vector< char Row ;
std::vector< Row display ;
Thing is I can never tell exactly how long the row could be. And the
version of snprintf on this platform returns -1 if printed string is
longer then buffer rather then what should be then length.
What I'd probably do is use something like the above. Then,
given a request to insert an int 'value' at position i, j, I'd
write something like:

std::ostringstream tmp ;
tmp << value ;
if ( display.size() <= i ) {
display.resize( i + 1 ) ;
}
Row& r = display[ i ] ;
std::string s( tmp.str() ) ;
size_t lastCol = j + s.size() ;
if ( r.size() <= lastCol ) {
r.resize( lastCol + 1, ' ' ) ;
}
std::copy( s.begin(), s.end(), r.begin() + j ) ;
I was hoping with seeking in a stringstream I could keep almost the
same arguments
Example of old stuff below:
pn1(0, row, "%-10s", HemName_of(obj));
pn1(11, row, "%-2s", Area_sh_nms[Mt_AreaFromCnum(Mt_Cnum_of(obj))]);
pn1(14, row, "%-8d", Mt_Cnum_of(obj));
pn1(23, row, "%-6s", Mt_ObjTypeName_of(obj));
pn1(30, row, "%-7s", Mt_CmdtyName_of(obj));
The only real problem will be handling the formatting strings.
And it shouldn't be too difficult to parse them, mapping them to
fmtflags, width and precision arguments for the ostringstream.
Or check out boost::format, which I think can handle about 95%
of the formatting flags---only a few of the more exotic cases
aren't handled. (The syntax of boost::format is rather wierd,
since it uses %, rather than <<, as the inserter, which rapidly
leads to fairly unreadable code. But if you're only inserting
one value at a time, and that value is always a parameter to the
function, it shouldn't be too bad. I used to have a Format
class myself; as a result of a challenge, I'd actually
implemented 100% of the printf formatting, plus the X/Open
extensions, along with all sorts of hooks to make it easy for
user defined types to do as well, if they had semantics more or
less like those of a floating point or integral number. I
stopped maintaining it sometime back, however, because I never
used it; the ostream formatting is so much more readable.)

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

Sep 15 '07 #8

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

Similar topics

4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
6
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\"...
5
by: Simon Pryor | last post by:
I am having some strange problems using std::ostringstream. The simple stuff works okay, but trying to use: std::ostringstream::str(const std::string&) or:...
3
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
3
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears...
3
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s...
3
by: Generic Usenet Account | last post by:
With the deprecated ostrstream class, when the constructor is invoked without arguments, memory is dynamically allocated. In that case the onus on freeing the memory lies with the user. Typically...
2
by: kaferro | last post by:
I use the following code to reset an ID when an order has been filled by more than one trade. For example, order= buy 9 corn, order ID = 101. If the order is filled with three separate trades of,...
11
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
1
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
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: 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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
0
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...
0
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....

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.