File Streaming Problem

Started by
9 comments, last by Bagpuss 22 years, 3 months ago
Quick question today, I am trying to use a filestream to output data in a binary mode. (Using Windows and MS Vis C++) this does not work, but it does go into the strOP.write instruction
  
	#include <fstream>
	#include <ios.h>
	#include <stdiostr.h>
	#include <fstream.h>

	int nMode = ios::out|ios::binary;

	std::ofstream strOp("C:\\matt.bin",nMode);

	if (strOp.is_open==0)
	{
		AfxMessageBox("Failed to open file ",MB_OK,0);
		failed = TRUE;
	}
	else
	{
		strOp.write("This is a test",14);
		strOp.close();
	}
  
But this code does. It opens a file as text output
  
	int nMode = ios::out;
	std::ofstream strOp("C:\\matt.bin",nMode);

	if (strOp.is_open==0)
	{
		AfxMessageBox("Failed to open file ",MB_OK,0);
		failed = TRUE;
	}
	else
	{
		strOp.write("This is a test",14);
		strOp.close();
	}
  
What is wrong and how do I open a file in Binary mode ? TIA, BP.
Advertisement
I always use <Stdio.h> for my file stuff :o).


  FILE *out;//Instead of using \\ you can use /out = fopen("C:/matt.bin","wb");if (out) //Open succesful?{//fwrite(void *,size_t,size_t,FILE*);/*The first size is the size of the data type you''re passing, in this case it''s 1 (sizeof(char)), but you can output an array of ints,shorts,float,etc,etc by using sizeof(type), and then the # of array elements.  I dunno, I hate using filestream''s, standard C is more efficient, and just as simple to read/understand.*/ fwrite("This is a test",1,14,out); fclose(out);}else{ AfxMessageBox("Failed to open file",MB_OK,0); failed = true;}  
do not mix the std headers with common headers
use or not both. try with std::ios::binary
humanity will always be slaved by its ignorance
Just out of curiosity, why are you opposed to "mixing the standard and common headers" ?

To answer the question. I have NO flipping idea why this matters, and I suspect its a VC++ bug, but you need to remove the namespace decleration from the beginning of :

std::ofstream

IE

ofstream strOp("C:\\matt.bin",nMode);


I tried your code and it never created the file. I took off the qualifier and it worked like a charm.

It worked fine both with the namespace decleration (std: and without in ASCII mode.

Nick
"



(Use if you're redeeming a promotional certificate or coupon.)

Returning customer?
Sign in to turn on 1-Click ordering.






(We'll set one up for you)
View my Wish List

Palm OS Game Programming" Available Feb. 2002

Edited by - enjolras on January 11, 2002 10:15:13 AM
The likely reason as to why you need to remove the std namespace declaration is because you included some of the headers with .h

the C++ standard library headers should all always be included as #include <fstream> #include <ios> etc.

adding the .h includes older versions of the headers ( such as non-template forms of iostreams ) and does not put things into the std namespace


As to why it is not working when you open in binary mode - a possible explanation is that you can''t really write a string to an iostream in binary mode and expect it to make a human readable file. - especially when you use the raw write() method

you should probably use the insertion operators -

eg

strOp << "This is a test";

which will properly write a string to the output stream.

I suggest looking up some info on iostreams - using inserters/extractors is a primary part of their design - it allows typesafe IO with formatting.
iostream.h, fstream.h, ios.h, blah...
All belong to an old implemetation of the C++ standard. The newest uses the classes in iostream, fstream, etc. The only (i think) *io*.h that is still the same is iosfwd.h. Even stdio.h turned into cstdio alogn with string.h (cstring), stdlib.h(cstdlib), ctype.h (cctype) because the old c headers are now cheadername and intrudeced into the std namespace.
humanity will always be slaved by its ignorance
sorry, i started this about 7am, and had to leave. It''s now 2:30pm. I''m sure your question has already been answered, but i''ll hit post anyways.

  #include <fstream>using namespace std;int main(){// get a file pointer and open the fileofstream fileOut;//fileOut.open("test.bin", ios::out | ios::in | ios::binary); // used for appendingfileOut.open("test.bin", ios::binary); // delets old file firstchar test[] = "this is a test";fileOut.write((char *)(&test), sizeof(test));fileOut << flush;fileOut.close();return 0;}  




"They that can give up essential liberty to obtain temporary safety deserve neither liberty nor safety."
- Benjamin Franklin
www.ChippedDagger.com"They that can give up essential liberty to obtain temporary safety deserve neither." -- Benjamin Franklin"If opportunity doesn't knock, build a door." -- Milton Berle
Yep: you don''t mix use of the ''.h'' extension headers with the headers that lack the ''h''. At best, they won''t work together and will give you compilation or linking errors, or at worst, they still compile fine and yet produce the wrong behaviour, which is what you have here. The ios:: flags you want are included for you when you #include fstream, so you don''t need to include ios anyway. Certainly not ios.h, which is the old library as previously stated.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
OK, thanks for all your input and help !

Bp.
i''ve had really bad experience with microsoft implementation of iostream, it wouldn''t do tellg, seekg, tellp or seekp properly, and those are simple fundamental things, i prefer C stdio, simply because its small, clean and efficient, it also works off the bat, no pissing around

This topic is closed to new replies.

Advertisement