C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
c++ IO streams or C FILE ??? Posted by Danty on 14 May 2006 at 9:37 PM
are C++ IO streams really used for binary IO? If so, any quick reference to examples (I guess I have to buy the book Standard C++ IOStreams and Locales) ??
whenever I try to use IO streams instead of good old C FILE* (although more work) i run into problems:

std::fstream fs(std::ios_base::in | std::ios_base::out);
fs << "hello world";
//  now i want to delete (purge) the entire file contents
// sure i can close/reopen and truncate....but dont' want to have to close file 

Report
Re: c++ IO streams or C FILE ??? Posted by Donotalo on 14 May 2006 at 10:42 PM
This message was edited by Donotalo at 2006-5-14 22:44:20

: are C++ IO streams really used for binary IO? If so, any quick reference to examples (I guess I have to buy the book Standard C++ IOStreams and Locales) ??
: whenever I try to use IO streams instead of good old C FILE* (although more work) i run into problems:
:
:
: std::fstream fs(std::ios_base::in | std::ios_base::out);
: fs << "hello world";
: //  now i want to delete (purge) the entire file contents
: // sure i can close/reopen and truncate....but dont' want to have to close file 
: 

:
C++ io fstreams can be used for binary io to file. for that purpose, u need to open the file in binary mode. a sample code:

fstream fs(ios::binary);

now fs is ready for binary input/output. fstreams open in ios::in | ios::out
mode by default. so i omitted that part.
[/purple]

~Donotalo()



Report
Re: c++ IO streams or C FILE ??? Posted by AsmGuru62 on 15 May 2006 at 4:39 AM
: are C++ IO streams really used for binary IO? If so, any quick reference to examples (I guess I have to buy the book Standard C++ IOStreams and Locales) ??
: whenever I try to use IO streams instead of good old C FILE* (although more work) i run into problems:
:
:
: std::fstream fs(std::ios_base::in | std::ios_base::out);
: fs << "hello world";
: //  now i want to delete (purge) the entire file contents
: // sure i can close/reopen and truncate....but dont' want to have to close file 
: 

:
FILE* is much simpler than streams and I suspect that streams built on top of good old FILE*, so they slower, obvioiusly.
Report
Re: c++ IO streams or C FILE ??? Posted by Griz803 on 15 May 2006 at 10:30 AM
: : are C++ IO streams really used for binary IO? If so, any quick reference to examples (I guess I have to buy the book Standard C++ IOStreams and Locales) ??
: : whenever I try to use IO streams instead of good old C FILE* (although more work) i run into problems:
: :
: :
: : std::fstream fs(std::ios_base::in | std::ios_base::out);
: : fs << "hello world";
: : //  now i want to delete (purge) the entire file contents
: : // sure i can close/reopen and truncate....but dont' want to have to close file 
: : 

: :

Search for seekp and / or tellp. These should be the equivalent of the seek and tell functions from FILE * management. This should let you move the existing file pointer to the beginning of the file, thus truncating without closing. Remember to check error conditions since this can get you massive runtimes if mishandled! I hope this helps.
Report
Re: c++ IO streams or C FILE ??? Posted by stober on 15 May 2006 at 1:15 PM
you also have to use stream's read() and write() methods, not the >> or << operators.

How streams are implemented is compiler dependent -- the compiler may, or may not, simply wrap FILE and associated functions. The standards do not dictate how streams are implemented -- but I think the Microsoft compiler do just wrap them with FILE functions, and FILE functions are a wrapper for win32 api functions. I don't know how *nix or MAC implements them.
Report
umm...still that's not Binary IO Posted by Danty on 15 May 2006 at 2:34 PM
ok, it seems that all of the above replies have same thoughts as I initially did.

using the ios_base::binary mode will not do it, it simply interperts "\n", "\r\n", at end of strings.... Even write()/read() will not really do true C's FILE* binary. To really do it I'd have to derive and implement from streambufs class.

then notion that i'm getting is that C++ IO streams are only for STRINGs.

check out this site, the best i've found so far (like i said, i think i need to buy that book :) )


see Binary IO section:
http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html

Report
Re: umm...still that's not Binary IO Posted by stober on 15 May 2006 at 5:37 PM
This message was edited by stober at 2006-5-15 17:43:48

I don't see the problems with binary i/o that are mentioned in the article you posted. If you use VC++ 2005 express, open a text file in binary mode then read the entire file into one huge buffer. After it is read, use the debugger to view all the bytes in the file -- you will notice that the data in the buffer is an exact copy of the data on the file. ifstream did not change the file one byte.

If you still don't believe it, compile and run this program with your compiler -- it writes and reads a true binary file, not mearly a text file treated as binary.
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
long a = 123;
long b = 456;
float c = 234.456f;

ofstream out("myfile.dat",ios::binary);
out.write((char *)&a,sizeof(long));
out.write((char *)&b,sizeof(long));
out.write((char *)&c,sizeof(float));
out.close();
a = b = 0;
c = 0.0;
ifstream in("myfile.dat", ios::binary);
in.read((char *)&a,sizeof(long));
in.read((char *)&b,sizeof(long));
in.read((char *)&c,sizeof(float));
in.close();
cout << "a = " << a << "\n";
cout << "b = " << b << "\n";
cout << "c = " << c << "\n";
	return 0;
}





Report
Re: umm...still that's not Binary IO Posted by Danty on 15 May 2006 at 8:24 PM
yeah that works, i guess what i ment to say was that C++ Binary IO mode is not well, standard like a text mode standards. i'd still have to take care of bit ordering, data type bit sizes ... if i were to write binary data files for different platforms.
Report
Re: umm...still that's not Binary IO Posted by stober on 16 May 2006 at 2:17 AM
: yeah that works, i guess what i ment to say was that C++ Binary IO mode is not well, standard like a text mode standards. i'd still have to take care of bit ordering, data type bit sizes ... if i were to write binary data files for different platforms.
:


yes and you have to do that with C's FILE functions too. binary files are not often easily transferrable between operating systems.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.