Grrr... C++ file I/O

S

SpreadTooThin

Damn.. FILE *fp was just sooo much easier than C++ file i/o...
I just don't get it.. really!

filebuf, istream, ostream, ifstream, ofstream, iosteam...
what the f&(){( do I use to do binary file i/o read and write.
I need to be able to read and write, seek and tell
which class do I use?????

frustrated..
 
O

osmium

SpreadTooThin said:
Damn.. FILE *fp was just sooo much easier than C++ file i/o...
I just don't get it.. really!

filebuf, istream, ostream, ifstream, ofstream, iosteam...
what the f&(){( do I use to do binary file i/o read and write.
I need to be able to read and write, seek and tell
which class do I use?????

Use ios::binary as a parameter the file open.
Use ifstream, tellg, seekg and read to read.
Use ofstream, tellp, seekp, and write to write.

Salt judiciously with std:: until your head swims.
 
R

Ron AF Greve

Hi,

Simple example

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main( int ArgC, char *ArgV[] )
{
//----------------Input example
// bin just to make sure it works the same on ms-windows and unix flavors
(unix'es are always bin)
ifstream Input( "Filename.dat", ios_base::binary );
if( !Input.is_open() )
{
cerr << __LINE__ << "see an error" << endl;
throw "Error";
}
string Line;
while( getline( Input, Line ) )
{
cout << "Read <" << Line << ">" << endl;
}

//-------------------Output example
ofstream Output( "Filenameout.dat", ios_base::binary );
if( !Output.is_open() )
{
cerr << __LINE__ << "see an error" << endl;
throw "Error";
}
// Right left alligned space right padded long as string
long Number = 10;
// The following writes text
Output << setiosflags( ios_base::left ) << resetiosflags(
ios_base::right ) << setw( 20 ) << setfill( ' ' ) << Number << endl;

// This wil write 4 bytes on most 32 bit machines
// Write arbitrary data for instance long
Output.write( reinterpret_cast<char*>( &Number ), sizeof( Number ) ); //
Opposite is Input.read


}

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
S

SpreadTooThin

Use ios::binary as a parameter the file open.
Use ifstream, tellg, seekg and read to read.
Use ofstream, tellp, seekp, and write to write.

Salt judiciously with std:: until your head swims.

Am I correct the tellg resets the stream to the begining of the file?
 
S

SpreadTooThin

Use ios::binary as a parameter the file open.
Use ifstream, tellg, seekg and read to read.
Use ofstream, tellp, seekp, and write to write.

Salt judiciously with std:: until your head swims.

The header files <iostream>, <fstream> etc..
I mean what is iostream for?
why not just fstream or ifstream for ifstream and ofstream for
ofstream?
 
O

osmium

SpreadTooThin said:
Am I correct the tellg resets the stream to the begining of the file?

No. I forgot to mention, g stands for get and p stand for put; there are
two pointers into the file. seekg(0) and seekp(0) will get you to the
beginning of the file, may be syntax errors, I am writing from memory.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Am I correct the tellg resets the stream to the begining of the file?

No, tellg() tells you where you are, if you want to go somewhere use
seekg(). For more documentation about streams have a look at
www.cplusplus.com
 
S

SpreadTooThin

No. I forgot to mention, g stands for get and p stand for put; there are
two pointers into the file. seekg(0) and seekp(0) will get you to the
beginning of the file, may be syntax errors, I am writing from memory.

But in no circumstance will tell reposition the file pointer?
 
J

James Kanze

No. I forgot to mention, g stands for get and p stand for put; there are
two pointers into the file. seekg(0) and seekp(0) will get you to the
beginning of the file, may be syntax errors, I am writing from memory.

No there aren't. In general, at the [io]stream level, it is
unspecified whether a bi-directional stream maintains two
pointers, or one. The interface is designed to support two,
since some streams (e.g. stringstream) do support two, but
changing the read position (seekg()) on a fstream will also
change the write position.

Be aware, too, that all of the restrictions as to what
positionning is legal on a FILE* also apply to [io]stream. In
practice, on a text stream, you can only seek to the beginning,
or to a place where you've been before, and whose position you
obtained using tell[gp]. (And of course tell[gp] never changes
the position in the file.)
 
R

Roland Pibinger

Then why don't you use it? It's not like it doesn't work in C++.

I always prefer stdio to iostream if I have the choice. It's also not
difficult to wrap FILE* and the functions into a safer class.
 
S

SpreadTooThin

Then why don't you use it? It's not like it doesn't work in C++.

I guess part of it is keeping up with the times (even if C++ is not
a spring chicken in terms of technological age.) and being able to
look at other peoples code and understand it. The same question may
be why C++ if you liked C. But yes I understand where you are comming
from.

B.
 
J

Juha Nieminen

Roland said:
I always prefer stdio to iostream if I have the choice.

The problem with stdio is that it's not abstract enough.
Example:

typedef int ValueType;
....
ValueType i = 4;
printf("%i\n", i); // this times 100 in a large program.

Later you notice that int is actually not enough, and you are
compiling for a 64-bit system, so you change:

typedef long ValueType;

and all your printfs break.

Ok, you could use a kludge and say:

typedef int ValueType;
#define VALUE_TYPE_FORMAT_STR "%i"
....
ValueType i = 4;
printf(VALUE_TYPE_FORMAT_STR"\n", i);

But then in the future you notice that actually int nor even long
is enough, so you replace the typedef with

class ValueType { ... }; // eg. a 256-bit integer

and all your printfs break.
 
A

Alan Johnson

James said:
Why would it work in C++? It didn't work in C, at least for
formatted IO.

Because in C++ you can wrap the FILE* in a class. Maybe even overload
an operator to automatically format the primitive types for you before
writing. And then clients could also overload that operator to format
their own types. Maybe operator<< would be a good choice.
 
R

Roland Pibinger

The problem with stdio is that it's not abstract enough.
Example:

You example indicates that stdio may not be type-safe enough (not that
it's not abstract enough).
typedef int ValueType;
...
ValueType i = 4;
printf("%i\n", i); // this times 100 in a large program.

Later you notice that int is actually not enough, and you are
compiling for a 64-bit system, so you change:

typedef long ValueType;

and all your printfs break.

Ok, you could use a kludge and say:

typedef int ValueType;
#define VALUE_TYPE_FORMAT_STR "%i"
...
ValueType i = 4;
printf(VALUE_TYPE_FORMAT_STR"\n", i);

Your integer value has a specific meaning in the current context which
could be expressed by a type-safe wrapper function, e.g.

inline
int printAmount (int i) {
return printf("%i\n", i);
}
But then in the future you notice that actually int nor even long
is enough, so you replace the typedef with

class ValueType { ... }; // eg. a 256-bit integer

and all your printfs break.

In theory you can write an operator<< for ValueType, in practice you
don't know a general purpose format to write one and need funtions for
different formatted outputs anyway.
 
J

Juha Nieminen

Roland said:
Your integer value has a specific meaning in the current context which
could be expressed by a type-safe wrapper function, e.g.

inline
int printAmount (int i) {
return printf("%i\n", i);
}

That only works if you want to print the value to stdout with that
exact format. What if you want to print it with another format (and
possibly alongside other values and text) and for example to a
char array instead of stdout?
 
R

Roland Pibinger

That only works if you want to print the value to stdout with that
exact format. What if you want to print it with another format (and
possibly alongside other values and text) and for example to a
char array instead of stdout?

It depends on the number of print requirements in your application.
Unless you need hundreds of different formats small wrapper functions
are appropriate. The 'backend' functions (printf, fprintf, snprintf)
are ready for use and wrappers are implemented quickly. At least I
prefer functions like printDateISO() etc.
 
L

Lionel B

I always prefer stdio to iostream if I have the choice. It's also not
difficult to wrap FILE* and the functions into a safer class.

I believe that has been done before... for one such implementation see
the standard fstream classes ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top