学习C++的输入输出,比起java来有种空虚的感觉,不会就这么少吧!转码也没见到!java是跨平台语言,之所以跨平台是因为java语言本身就带着一个平台,java运行在虚拟机之上;灵活是以不灵活为基础的,总会找到不灵活的那个点吧;C++标准库的输入输出是通过什么来实现的?操作系统?(若是的话所有商业操作系统必须给按照c++规范供它接口),bois?这个实现太复杂了吧!
这里想了解一下string和c的string的区别
c的string就是char数组不过,最后一位是'\0',为了兼容C语言,C++中所有的字符串面值都由编译器自动在末尾添加一个空字符串
1 char *c_string = "fgd";
2 char char_array[] = {'f', 'g', 'd', '\0'};
如果char_array的最后一位不用'\0',那么把char_array当做c字符串时,它将从char_array开始顺序内存地址知道遇到'\0'的前一位认为是这个字符串的最后一位。
《c++primer》"因为历史原因以及为了与c语言兼容,字符串字面值与标准库string类型不是同一种类型。这一点很容易引起混乱,编程时一定要注意区分字符串字面值和string数据类型的使用,这点很重要"
标准库中的string到底是种什么样的数据结构呢?好吧承认看不懂string的源码!那么baidu baidu不出来google一下->stackoverflow.com:c string的前几条不写了,关键一条:to determine the text's length, the array has to be scanned, character by character, fo a \0 character.
A string(c++不是c string) is a class that contains a char array, but automatically manages it for you. Most string implements hava a built-in array of 16 characters(so short string do not fragment(使成碎片) the heap) and use the heap for longer string.
C++ strings can contain embeded \0 characters, know their length without counting(通过什么方式不用计算就能知道它的length?string中要设置一个保存长度的内存?), are faster than heap-allocated char arrays for short texts and protect you form buffer overruns.Plus they are more readable and easier to use.
However, C++ strings are not(very) suitable for usage across DLL boundaries, because this would require any user of such a DLL function to make sure he's using the exact same compiler and C++ runtime implementation, lest(以免) he risk his string class behaving differently.
Normally, a string class would also release its heap memory on the calling heap, so it will only be able to free memory again if you're using a shared(.dll or .so)version of the runtime.
In short:use C++ string in all your internal functions and methods.if you ever write a .dll or .so, use C string in your public functions.
不过下面有人反对上面这段关于DLL中用string的看法“I do not believe the bit about DLL boundries. Under very special curcustances it could potentially break((one DLL is statically linking against a different version of the runtime than used by other DLL's) and worse things would probably happen first in these situations) but in the general case where everybody is using the default sharedd version of the standard runtime(the default) this will not happen”;
喜欢反对意见!从小到大,心中形成一种非对即错的观念;导致有时非得去想要一本“内容完全正确的书”,或其他;错误就真的那么不好吗? 做个数学题非得做完全对才完美?
用文本模式和二进制模式打开的区别:在文本模式中回车被当成一个字符"\n",而二进制模式认为它是两个字符0x0D,0x0A;如果在文件中读到ox1B,文本模式会认为是文件结束符,也就是二进制模型不会对文件进行处理,而文本方式会按一定的方式对数据作相应的转换。这段文字等到用到再去体会一下(从被人博客抄下来的)。
FILE I/O(binary)
Basic Model for File I/O
In C++, the file stream classes are designed with the idea that a file should simply be viewed as a stream or array of uninterpreted bytes. For convenience, the "array" of bytes stored in a file is indexed from zero to len-1, where len is the total number of bytes in the entire file.
Each open file has two "positions" associated with it:
1.The current reading position, which is the index of the next byte that will be read from the file. This is called the "get pointer" since it points to the next character that the basic get method will return.
2.The current writing position, which is the index of the byte location where the next output byte will be placed. This is called the "put pointer" since it points to the locaiton where the basic put method will place its parameter.
Opening a File -> Reading From a File -> Repositioning(使复位;改变…的位置) the "Get" Pointer (Writing To a File -> Repositioning the "Put" Pointer) (Reading and Writing Complex Data) -> Closing a File
A file stream object can be opened in one of two ways. First, you can supply a file name along with an i/o mode parameter to the constructor when declaring an object:
ifstream myFile("data.bin", ios::in | ios::binary);
Alternatively, after a file stream object has been declared, you can call its open method:
ofstream myFile;... myFile.open ("data2.bin", ios::out | ios::binary);
In order to manipulate binary files, you should always specify the i/o mode, including ios::binary as one of the mode flags.
看到文档中没有用 << >> 原以为是不是文档过时了!Normally, for binary file i/o you do not use the conventional text-oriented << and >> operatrors! It can be done, but that is an advanced topic.(操作符重载吧!)
下面只看一下它的Repositioning the "Put" Pointer
To change the position of the "put" pointer (the file reading position) of an fstream or ofstream object, use the seekp method. The basic form of this operation takes a single parameter:
ostream& seekp(streampos pos);
A streampos is essentially an unsigned long integer value. seekp moves the put pointer to the specified absolute file postion(where o is the start of the file). You can determine the current put pointer position using "myFile.tellp", a method with no parameters that returns the index of the put pointer on the given stream. There is also a variant of seekp that allows you to specify a positon relative to the current put pointer locaiton, or relative to the end of the file.
The Difference Between Binary and Text Files
The difference is that text files contain lines (or records) of text and each of these has an end-of-line marker automatically appended to the end of it whenever you indicate that you have reached the end of a line. There is an end of line at the end of the text written with the C fwrite() function or in C++ when you << endl. Binary files are not broken up into separate lines or records so the end-of line marker is not written when writing to a binary file.
未完,待用到再补充。发现写的东西太混乱,自己一步步学习想的过程,结果没整理清晰就顺着思路写下来了!能不能以后写的时候学完,有了整体概念,再写出来?
write by fgd