File Names in C++

C++ Primer 4/e在File Input and Output这个地方有一个警告:‘For historical reasons, the IO library uses C-style character strings (Section 4.3, p. 130) rather than C++ strings to refer to file names. When we call open or use a file name as the initializer when creating an fstream object, the argument we pass is a C-style string, not a library string. Often our programs obtain file names by reading the standard input. As usual, it is a good idea to read into a string, not a C-style character array. Assuming that the name of the file we wish to use is in a string, we can use the c_str member (Section 4.3.2, p. 139) to obtain a C-style string.’

中文版的这样写:‘由于历史因素,IO程式库使用C-style字元字串(4.3节.p.130)表示档案名称,而非使用C++ strings。当我们呼叫open()或在建立fstream物件时以档名为初值,传递时的引数都必须是C-style字串而不是程式库定义的strings。应用程式经常需要从标准输入装置取得档名,把它读入string是个好主意,不要读入C-style字元array:假设我们想使用的档名内含于某个string,可运用string提供的c_str()(4.3.2节,p.139)获得C-style字串。’

对喔!之前一直读到C-style的字串的问题,现在在file IO看到有差异的地方了,看来规定还是相沿已久来的传统,不得不签救救习惯啊!