Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Grrr... C++ file I/O

Reply
Thread Tools

Grrr... C++ file I/O

 
 
SpreadTooThin
Guest
Posts: n/a
 
      06-08-2007
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..

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      06-08-2007
"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.


 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      06-08-2007
SpreadTooThin <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ron AF Greve
Guest
Posts: n/a
 
      06-08-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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..
>



 
Reply With Quote
 
SpreadTooThin
Guest
Posts: n/a
 
      06-08-2007
On Jun 8, 3:00 pm, "osmium" <(E-Mail Removed)> 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?


 
Reply With Quote
 
SpreadTooThin
Guest
Posts: n/a
 
      06-08-2007
On Jun 8, 3:00 pm, "osmium" <(E-Mail Removed)> 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?

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      06-08-2007
"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.


 
Reply With Quote
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      06-08-2007
On 2007-06-08 23:55, SpreadTooThin wrote:
> On Jun 8, 3:00 pm, "osmium" <(E-Mail Removed)> 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
 
Reply With Quote
 
SpreadTooThin
Guest
Posts: n/a
 
      06-08-2007
On Jun 8, 4:48 pm, "osmium" <(E-Mail Removed)> 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?

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-08-2007
On Jun 9, 12:48 am, "osmium" <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting JPG file ppt file [Powerpoint] file zxcvar Digital Photography 7 06-22-2009 07:54 PM
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
How to create MDF file (.mdf) file from XML file. Dave ASP .Net 1 06-07-2007 11:32 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muñoz ASP .Net 3 06-04-2005 10:56 AM



Advertisments