Grrr... C++ file I/O - c++
This is a discussion on Grrr... C++ file I/O - c++ ; 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 ...
-
Grrr... C++ file I/O
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..
-
Re: Grrr... C++ file I/O
"SpreadTooThin" writes:
> 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.
-
Re: Grrr... C++ file I/O
SpreadTooThin <bjobrien62@> writes:
>Damn.. FILE *fp was just sooo much easier than C++ file i/o...
>I just don't get it.. really!
http://www-h.eng.cam.ac.uk/help/tpl/...es/C++/io.html
*might* help.
-
Re: Grrr... C++ file I/O
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
"SpreadTooThin" <bjobrien62@> wrote in message
news:1181335720.475267.259460@i13g2000prf.googlegroups.com...
> 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..
>
-
Re: Grrr... C++ file I/O
On Jun 8, 3:00 pm, "osmium" <r124c4u...@comcast.net> wrote:
> "SpreadTooThin" writes:
> > 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.
Am I correct the tellg resets the stream to the begining of the file?
-
Re: Grrr... C++ file I/O
On Jun 8, 3:00 pm, "osmium" <r124c4u...@comcast.net> wrote:
> "SpreadTooThin" writes:
> > 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.
The header files <iostream>, <fstream> etc..
I mean what is iostream for?
why not just fstream or ifstream for ifstream and ofstream for
ofstream?
-
Re: Grrr... C++ file I/O
"SpreadTooThin" writes:
> 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.
-
Re: Grrr... C++ file I/O
On 2007-06-08 23:55, SpreadTooThin wrote:
> On Jun 8, 3:00 pm, "osmium" <r124c4u...@comcast.net> wrote:
>> "SpreadTooThin" writes:
>> > 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.
>
> 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
--
Erik Wikström
-
Re: Grrr... C++ file I/O
On Jun 8, 4:48 pm, "osmium" <r124c4u...@comcast.net> wrote:
> "SpreadTooThin" writes:
> > 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.
But in no circumstance will tell reposition the file pointer?
-
Re: Grrr... C++ file I/O
On Jun 9, 12:48 am, "osmium" <r124c4u...@comcast.net> wrote:
> "SpreadTooThin" writes:
> > 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.
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.)
--
James Kanze (Gabi Software) email: james.kanze@
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
Similar Threads
-
By Application Development in forum PHP
Replies: 2
Last Post: 12-07-2007, 02:37 PM
-
By Application Development in forum lisp
Replies: 6
Last Post: 10-25-2007, 02:20 PM
-
By Application Development in forum c++
Replies: 1
Last Post: 07-09-2007, 09:47 PM
-
By Application Development in forum basic.visual
Replies: 3
Last Post: 01-22-2007, 07:42 PM
-
By Application Development in forum Java
Replies: 0
Last Post: 02-03-2004, 06:00 AM