fstream ios_base::binary

Hi again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define NO_ERROR 0

#include <fstream>


using namespace std;


int main(int argc, char * argv[]){
	
	


	char output[200];
	int   ioutput[200];
	
	for(int i=0; i<200; i++){
		output[i]='1';
		ioutput[i]=49;	
	}

	ofstream myFile;

	myFile.open("config.txt");
	myFile.write(output, 200);
	myFile.close();

	ofstream binary;

	binary.open("config.bin", ios_base::binary);
	binary.write((char*)&ioutput[0], (sizeof(int)*200));
	binary.close();


	return NO_ERROR;									
};


Now, this should create two files, one wrote normaly and the other in binary mode. The question is, why is second one so much bigger? sizeof(int)* 200 seems like the reason, but I bet I can write config.bin smaller (it's 800 bytes of size, config.txt is 200)

Ok, so I wrote same thing two times, just the second one is a complete waste of space, 4 times bigger...

question changes: How could I write this in binary code, for faster loading?

Thanks,

Gregor
Last edited on
The answer to your question is simple: Your so-called "binary file" is four times bigger than the first one because you write four times the characters than you do in the first file since sizeof(int) appears to be 4 on your machine/system.

The question I unfortunately couldn't answer myself (and I'm very excited to hear the answer to), is: What the heck is this code supposed to do?
It's the code I wrote while learning about streams. As I wrote, I figured what ios_base::binary actualy does so I guess this would be it.

Thanks
No, ios::binary (and usually it's ios::binary, not ios_base::binary) is required on system that draw a distinction between text and binary files, e.g. Windows. But tell me, what is so _binary_ about your second file? Just because you changed the file extension doesn't mean, the mime type changed.
I Know. What I dont know is how could I save files, so they could load faster...this is all I wanted to do from the start, but I didnt know much about ios::binary so it was all wierd and totaly not what I've wanted.

Hope we're clear now, if not ask away:)

Thanks!
They didn't load faster in which program? File format? ASCII text? Depends pretty much on your machine/os how fast they're loaded.
Topic archived. No new replies allowed.