| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"maria" |
| Date: |
13 Jan 2004 09:40:25 PM |
| Object: |
i/o Fortran's format commands in c++ |
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
I would be gratefull for any help.
Thank you
.
|
|
| User: "David Harmon" |
|
| Title: Re: i/o Fortran's format commands in c++ |
13 Jan 2004 11:04:38 PM |
|
|
On 13 Jan 2004 19:40:25 -0800 in comp.lang.c++,
(maria) was alleged to have written:
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
The printf(), scanf(), etc. library functions, that C++ inherits
from C, provide formatted I/O somewhat reminiscent of Fortran formatted
I/O. Most C++ programmers prefer the standard iostreams library for
type safety and extensibility.
See the section "[15] Input/output via <iostream> and <cstdio>" in
Marshall Cline's C++ FAQ. It is always good to check the FAQ before
posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
.
|
|
|
| User: "Nick Hounsome" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 12:45:06 AM |
|
|
"David Harmon" <source@netcom.com> wrote in message
news:4018cb98.33199246@news.west.earthlink.net...
On 13 Jan 2004 19:40:25 -0800 in comp.lang.c++,
(maria) was alleged to have written:
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
The printf(), scanf(), etc. library functions, that C++ inherits
from C, provide formatted I/O somewhat reminiscent of Fortran formatted
I/O. Most C++ programmers prefer the standard iostreams library for
type safety and extensibility.
Do they really?
I think it depends on what you mean by formatted. Personally I find them
extremely clunky for doing what I usualy think of
as formatted I/O i.e. the difference between 0x%06x and %d.
Putting aside the biggest problem which is properly saving and restoring the
base and fill character, including iomanip,
upper versus lower case hex letters and the std namespace you still have:
cout << x << hex << "0x" << setw(6) << setfill('0') << y << dec;
versus
printf("%d 0x%06x",x,y);
I think that the standard should actualy supply a class to save and restore
all this 'stuff' automatically in ctor/dtor e.g.
std::format_state fs(cout);
This is in no way intended to deny that they are wonderful for printing
classes where C has nothing.
See the section "[15] Input/output via <iostream> and <cstdio>" in
Marshall Cline's C++ FAQ. It is always good to check the FAQ before
posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
.
|
|
|
| User: "Jeff Flinn" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 08:50:29 AM |
|
|
"Nick Hounsome" <nh002@blueyonder.co.uk> wrote in message
news:Hy5Nb.604$M26.453@news-binary.blueyonder.co.uk...
"David Harmon" <source@netcom.com> wrote in message
news:4018cb98.33199246@news.west.earthlink.net...
On 13 Jan 2004 19:40:25 -0800 in comp.lang.c++,
(maria) was alleged to have written:
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
The printf(), scanf(), etc. library functions, that C++ inherits
from C, provide formatted I/O somewhat reminiscent of Fortran formatted
I/O. Most C++ programmers prefer the standard iostreams library for
type safety and extensibility.
Do they really?
I think it depends on what you mean by formatted. Personally I find them
extremely clunky for doing what I usualy think of
as formatted I/O i.e. the difference between 0x%06x and %d.
Putting aside the biggest problem which is properly saving and restoring
the
base and fill character, including iomanip,
upper versus lower case hex letters and the std namespace you still have:
cout << x << hex << "0x" << setw(6) << setfill('0') << y << dec;
versus
printf("%d 0x%06x",x,y);
I think that the standard should actualy supply a class to save and
restore
all this 'stuff' automatically in ctor/dtor e.g.
std::format_state fs(cout);
See http://www.boost.org/libs/io/doc/ios_state.html which saves the state as
you desire. If you still insist on printf, see
http://www.boost.org/libs/format/index.htm for a typesafe version.
Jeff F
.
|
|
|
| User: "Nick Hounsome" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 11:28:18 AM |
|
|
"Jeff Flinn" <NONONE@nowhere.com> wrote in message
news:bu3kvm$jng$1@bluegill.adi.com...
"Nick Hounsome" <nh002@blueyonder.co.uk> wrote in message
news:Hy5Nb.604$M26.453@news-binary.blueyonder.co.uk...
"David Harmon" <source@netcom.com> wrote in message
news:4018cb98.33199246@news.west.earthlink.net...
On 13 Jan 2004 19:40:25 -0800 in comp.lang.c++,
(maria) was alleged to have written:
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
The printf(), scanf(), etc. library functions, that C++ inherits
from C, provide formatted I/O somewhat reminiscent of Fortran
formatted
I/O. Most C++ programmers prefer the standard iostreams library for
type safety and extensibility.
Do they really?
I think it depends on what you mean by formatted. Personally I find them
extremely clunky for doing what I usualy think of
as formatted I/O i.e. the difference between 0x%06x and %d.
Putting aside the biggest problem which is properly saving and restoring
the
base and fill character, including iomanip,
upper versus lower case hex letters and the std namespace you still
have:
cout << x << hex << "0x" << setw(6) << setfill('0') << y << dec;
versus
printf("%d 0x%06x",x,y);
I think that the standard should actualy supply a class to save and
restore
all this 'stuff' automatically in ctor/dtor e.g.
std::format_state fs(cout);
See http://www.boost.org/libs/io/doc/ios_state.html which saves the state
as
you desire. If you still insist on printf, see
http://www.boost.org/libs/format/index.htm for a typesafe version.
Jeff F
Great but you will see that I said "I think that the standard should actualy
supply ...."
.
|
|
|
| User: "Jeff Flinn" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 01:31:57 PM |
|
|
....
Putting aside the biggest problem which is properly saving and
restoring
the
base and fill character, including iomanip,
upper versus lower case hex letters and the std namespace you still
....
See http://www.boost.org/libs/io/doc/ios_state.html which saves the
state
as
you desire. If you still insist on printf, see
http://www.boost.org/libs/format/index.htm for a typesafe version.
Great but you will see that I said "I think that the standard should
actualy
supply ...."
Considering the large number of facilities from boost that have been
proposed in TR1, perhaps with a little prodding the ios_state could be
proposed for TR2, then your wish could be reality.
Jeff F
.
|
|
|
|
| User: "tom_usenet" |
|
| Title: Re: i/o Fortran's format commands in c++ |
15 Jan 2004 10:10:30 AM |
|
|
On Wed, 14 Jan 2004 17:28:18 -0000, "Nick Hounsome"
<nh002@blueyonder.co.uk> wrote:
Great but you will see that I said "I think that the standard should actualy
supply ...."
First write it, then use it, then it will be standardized.
Standardization is about standardizing existing practice, not
inventing new things. Standardization committees tend to get it wrong
when they try to invent things...
Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
.
|
|
|
|
|
|
| User: "Old Wolf" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 03:40:58 PM |
|
|
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
The printf(), scanf(), etc. library functions, that C++ inherits
from C, provide formatted I/O somewhat reminiscent of Fortran formatted
I/O. Most C++ programmers prefer the standard iostreams library for
type safety and extensibility.
Personally I find them extremely clunky
cout << x << hex << "0x" << setw(6) << setfill('0') << y << dec;
Or, if you follow the advice of some, it's even worse:
std::cout << x << std::hex << "0x" << std::setw(6)
<< std::setfill('0') << y << std::dec;
versus
printf("%d 0x%06x",x,y);
This is perfectly good and correct C++, nobody is forcing you to
use iostreams. If you want to avoid memory-allocation with sprintf
in general, write a small function that uses vsnprintf() to put the
output in a std::string instead of a char* . If you want type-safety,
use boost::format (although I avoid this because it slows down the
build a lot, without precompiled headers).
.
|
|
|
| User: "Jeff Flinn" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 03:51:52 PM |
|
|
"Old Wolf" <oldwolf@inspire.net.nz> wrote in message
news:843a4f78.0401141340.1a59d643@posting.google.com...
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
The printf(), scanf(), etc. library functions, that C++ inherits
from C, provide formatted I/O somewhat reminiscent of Fortran
formatted
I/O. Most C++ programmers prefer the standard iostreams library for
type safety and extensibility.
Personally I find them extremely clunky
cout << x << hex << "0x" << setw(6) << setfill('0') << y << dec;
Or, if you follow the advice of some, it's even worse:
std::cout << x << std::hex << "0x" << std::setw(6)
<< std::setfill('0') << y << std::dec;
versus
printf("%d 0x%06x",x,y);
This is perfectly good and correct C++, nobody is forcing you to
Without type-safety I'd venture to say that printf is not "perfectly good"
and not "perfectly correct.
use iostreams. If you want to avoid memory-allocation with sprintf
in general, write a small function that uses vsnprintf() to put the
output in a std::string instead of a char* . If you want type-safety,
How does this avoid memory allocation?
Jeff F
.
|
|
|
| User: "Nick Hounsome" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 04:22:25 PM |
|
|
"Jeff Flinn" <NONONE@nowhere.com> wrote in message
news:bu4dlq$ae7$1@bluegill.adi.com...
[snip]
Without type-safety I'd venture to say that printf is not "perfectly good"
and not "perfectly correct.
gnu compilers check all the printf args for you (provided the format string
is a literal)
I know it's not a language thing but it is pretty helpful.
.
|
|
|
|
|
| User: "Robert Paul Clark" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 05:04:55 PM |
|
|
Look at:
ftp://www.instructor.com/CPPClass/CPPClassDirTree/Solution/FRI/MPRINTF.H
It's a C++ class that basically wraps printf() so that you can call it as a
function and use it with cout.
Ex:
cout << mprintf("Hey %d", 2);
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
The printf(), scanf(), etc. library functions, that C++ inherits
from C, provide formatted I/O somewhat reminiscent of Fortran formatted
I/O. Most C++ programmers prefer the standard iostreams library for
type safety and extensibility.
Personally I find them extremely clunky
cout << x << hex << "0x" << setw(6) << setfill('0') << y << dec;
Or, if you follow the advice of some, it's even worse:
std::cout << x << std::hex << "0x" << std::setw(6)
<< std::setfill('0') << y << std::dec;
versus
printf("%d 0x%06x",x,y);
This is perfectly good and correct C++, nobody is forcing you to
use iostreams. If you want to avoid memory-allocation with sprintf
in general, write a small function that uses vsnprintf() to put the
output in a std::string instead of a char* . If you want type-safety,
use boost::format (although I avoid this because it slows down the
build a lot, without precompiled headers).
.
|
|
|
| User: "Nick Hounsome" |
|
| Title: Re: i/o Fortran's format commands in c++ |
15 Jan 2004 12:33:08 AM |
|
|
"Robert Paul Clark" <bobc@lanl.gov> wrote in message
news:4005CB17.F01AF1C7@lanl.gov...
Look at:
ftp://www.instructor.com/CPPClass/CPPClassDirTree/Solution/FRI/MPRINTF.H
It's a C++ class that basically wraps printf() so that you can call it as
a
function and use it with cout.
Ex:
cout << mprintf("Hey %d", 2);
This is a disaster - I looked at it and it uses vsprintf into an auto (don't
use the keyword auto) 1K buffer.
Do you work for Microsoft by any chance?
.
|
|
|
| User: "Old Wolf" |
|
| Title: Re: i/o Fortran's format commands in c++ |
15 Jan 2004 02:31:48 PM |
|
|
It's a C++ class that basically wraps printf() so that you can call it as
function and use it with cout.
Ex:
cout << mprintf("Hey %d", 2);
This is a disaster - I looked at it and it uses vsprintf into an auto (don't
use the keyword auto) 1K buffer.
Do you work for Microsoft by any chance?
Try this one
(warning 1: untested code follows)
(warning 2: this will fail miserably if your implementation of
vsnprintf is broken - test your compiler's one first,
or download a free one from somewhere and check it)
#include <string>
#include <cstdio> // or wherever you have vsnprintf()
#include <cstdarg>
std::string str_printf(const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
int size = vsnprintf(NULL, 0, fmt, ap);
if (size < 1) return std::string();
char *buf = new char[size+1];
va_end(ap);
va_start(ap, fmt);
vsnprintf(buf, size+1, fmt, ap);
std::string ret(buf);
delete [] buf;
return ret;
}
There is the speed disadvantage (ie. it formats everything twice to work
out the amount of memory needed), so engage your brain if you
are going to call this in a bottleneck situation.
.
|
|
|
|
| User: "Robert Paul Clark" |
|
| Title: Re: i/o Fortran's format commands in c++ |
15 Jan 2004 10:06:06 AM |
|
|
I have no association with the code. But, I'd say that the author thoroughly
documents the 1K buffer size and attempts to report buffer overflows.
Any further whining on your part should probably be directed at the author -
indicated in the source. :-)
Nick Hounsome wrote:
"Robert Paul Clark" <bobc@lanl.gov> wrote in message
news:4005CB17.F01AF1C7@lanl.gov...
Look at:
ftp://www.instructor.com/CPPClass/CPPClassDirTree/Solution/FRI/MPRINTF.H
It's a C++ class that basically wraps printf() so that you can call it as
a
function and use it with cout.
Ex:
cout << mprintf("Hey %d", 2);
This is a disaster - I looked at it and it uses vsprintf into an auto (don't
use the keyword auto) 1K buffer.
Do you work for Microsoft by any chance?
.
|
|
|
|
|
|
| User: "Simon Elliott" |
|
| Title: Re: i/o Fortran's format commands in c++ |
17 Jan 2004 03:59:42 AM |
|
|
Old Wolf <oldwolf@inspire.net.nz> writes
This is perfectly good and correct C++, nobody is forcing you to
use iostreams. If you want to avoid memory-allocation with sprintf
in general, write a small function that uses vsnprintf() to put the
output in a std::string instead of a char* .
I've never used vsnprintf(). It looks useful. It seems that Borland C++
Builder 3 doesn't support it (though C++ Builder 6 does.)
How widespread is support for vsnprintf?
Is it now part of the standard C library?
Is there any public domain source for it?
--
Simon Elliott
http://www.ctsn.co.uk/
.
|
|
|
| User: "Jacques Labuschagne" |
|
| Title: Re: i/o Fortran's format commands in c++ |
17 Jan 2004 04:04:30 AM |
|
|
Simon Elliott wrote:
Is it now part of the standard C library?
Yes, in C99.
Is there any public domain source for it?
The GCC source would include it.
Jacques.
.
|
|
|
|
|
|
| User: "Rob Williscroft" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 12:29:13 PM |
|
|
Nick Hounsome wrote in
news:Hy5Nb.604$M26.453@news-binary.blueyonder.co.uk:
I think that the standard should actualy supply a class to save and
restore all this 'stuff' automatically in ctor/dtor e.g.
std::format_state fs(cout);
void f( int i )
{
std::ostream os( cout.rdbuf() );
os << std::hex << i;
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
.
|
|
|
| User: "Nick Hounsome" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 01:36:34 PM |
|
|
"Rob Williscroft" <rtw@freenet.REMOVE.co.uk> wrote in message
news:Xns9470BBE4DAD52ukcoREMOVEfreenetrtw@195.129.110.201...
Nick Hounsome wrote in
news:Hy5Nb.604$M26.453@news-binary.blueyonder.co.uk:
I think that the standard should actualy supply a class to save and
restore all this 'stuff' automatically in ctor/dtor e.g.
std::format_state fs(cout);
void f( int i )
{
std::ostream os( cout.rdbuf() );
os << std::hex << i;
}
not bad!
probably a bit heavyweight but it will do the job.
thanks for the tip.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
.
|
|
|
|
|
| User: "David Harmon" |
|
| Title: Re: i/o Fortran's format commands in c++ |
14 Jan 2004 03:23:03 AM |
|
|
On Wed, 14 Jan 2004 06:45:06 -0000 in comp.lang.c++, "Nick Hounsome"
<nh002@blueyonder.co.uk> was alleged to have written:
I/O. Most C++ programmers prefer the standard iostreams library for
type safety and extensibility.
Do they really?
Yes.
I think it depends on what you mean by formatted.
Formatted means converted to or from a legible character representation.
printf() and scanf() are formatted. read() and write() are raw or
unformatted. <iostream>'s operator<<() and operator>>() are formatted.
Personally I find them
extremely clunky for doing what I usualy think of
as formatted I/O i.e. the difference between 0x%06x and %d.
Both of those are formatted. Come on, this isn't rocket science.
.
|
|
|
|
|
|
| User: "Jacques Labuschagne" |
|
| Title: Re: i/o Fortran's format commands in c++ |
13 Jan 2004 10:40:47 PM |
|
|
maria wrote:
I 've decided it's time to move from fortran to c++, but i cant find
out if c++ supposrts I/O format commands like the ones in fortran 77.
I would be gratefull for any help.
Thank you
You assume we know Fortran. Why not describe how the IO should behave
first?
Jacques
.
|
|
|
|
| User: "E. Robert Tisdale" |
|
| Title: Re: i/o Fortran's format commands in c++ |
13 Jan 2004 11:05:55 PM |
|
|
maria wrote:
I 've decided that it's time to move from Fortran to C++
but I can't find out if C++ supports I/O format commands
like the ones in fortran 77.
Why can't you find out?
Do you have a C++ text book?
The C computer programming language has format *strings*
PRINTF(3) Linux Programmer’s Manual PRINTF(3)
NAME
printf, fprintf, sprintf, snprintf, vprintf, vfprintf,
vsprintf, vsnprintf - formatted output conversion
SYNOPSIS
#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
.
.
.
EXAMPLES
To print pi to five decimal places:
#include <math.h>
#include <stdio.h>
fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));
To print a date and time in the form ‘Sunday, July 3, 10:02’,
where weekday and month are pointers to strings:
#include <stdio.h>
fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
weekday, month, day, hour, min);
.
.
.
.
|
|
|
|

|
Related Articles |
|
|