Well, like most of my little itty bitty things I start with C++, they always involve Fstream.h, but my one little issue with it is how binary mode works exactly. AKA : Code #include <fstream> #include <iostram> int num=0; main() { cout<<"insert number\n"; cin>>num; ofstream file; file.open(file.bin, ios::binary) file<<num; } What would file.bin contain? lets say I input 2. would it be 00000010? if so, what if the example became: Code #include <fstream> #include <iostram> char letter[20]; main() { cout<<"insert phrase\n"; cin>>letter; ofstream file; file.open(file.bin, ios::binary) file<<letter; } ? would it convert it to ASCII and then to binary? Isn't that the same as text mode? *WARNING: due to cosmic radiation, Quantum fluctuations, or earth going into sector x79352C, the world may end at any moment. Have a nice day. | Current Project: Status:Complete Previous project status: Failed/Quit Current Issue C++ : fsteam help |
![]() |
Quote
so if I just took a ridiculous ammount of bytes and put it in for the 2nd argument of the file.write(), it would read that my bytes regardless of spaces?
That would segmentation fault (ie. crash); your program would attempt to read that many bytes from memory (and into the file) and then the OS would terminate it for attempting to access memory that it doesn't have permission to. That is, assuming the first argument is an invalid pointer or a pointer to some data that isn't as big as the size (second argument).
Code
file.write((const char*) 0xDEADBEEF, 32766); //segfault char *somedata = new char[28]; file.write(somedata, 24000); //segfault, when it tries to read somedata + 28 delete somedata; //the program will never get here!
About the text vs binary modes, use text mode for files that will contain only text (TXT, HTML/XML, C/C++/C#/Java source files, etc...) and binary mode for everything else (ROMs, databases, image/sound/video files, archive files (ZIP/7z/RAR/TAR.GZ), etc...). What text mode does is convert C/C++ newlines ("\n", aka Line Feed (0x0A in Hex)) to and from DOS/Windows newlines ("\r\n", aka Carriage Return + Line Feed (0x0D0A in Hex)), or whatever the standard newline is on the operating system you are compiling for. This can create havoc when working with binary data, because if you write data containing the binary representation of a newline, it will mess it up.
If you are writing code for *nix operating systems (Linux, BSD, Solaris, OS X), and only *nix, binary mode and text mode do the exact same thing, as on those platforms, Line Feed is the native newline. However, on Windows, if you use binary mode to write text files, if you open up the text files in Notepad all newlines (\n) will show up as rectangles instead of new lines, and therefore the file will be one long line of text. (If you use Wordpad, or a decent text editor (eg Notepad++) it will read it just as it would have if you had written to the file in text mode, as these editors understand *nix newlines.) It's better to use text mode for text files for this reason.
Your layout has been removed.