`
zengsai
  • 浏览: 27606 次
  • 性别: Icon_minigender_1
  • 来自: 成都
最近访客 更多访客>>
社区版块
存档分类
最新评论

ifstream 没有 ios::nocreate 属性

iOS 
阅读更多
<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

今天在 vs2008 中运行一个小程序的时候,发现了一个小问题,ifstream 的 open 函数中不能用 ios::nocreate 属性。详细见代码:

#include <fstream>
#include <iostream>
#include <stdlib><span style="color: #0000ff">int</span> main()
{
	<span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
	
	ifstream inFile;

	inFile.open("<span style="color: #8b0000">my.dat</span>",ios::in | ios::nocreate);
	<span style="color: #0000ff">if</span>(!inFile)
	{
		cerr 不能打开my.dat" return 0;

}</stdlib></iostream></fstream>

出现错误:

error C2065: “nocreate”: 未声明的标识符
error C2065: “inFile”: 未声明的标识符

原因:从 vs 2003 开始,微软用一个新的 iostream 替换了原来的。新包中没有 nocreate 标识符。

原来 ifstream 中的 open 函数默认打开就是不创建文件。如果没有这个文件则打开出错,而不是创建文件。详见代码运行结果:

#include <fstream>
#include <iostream>
#include <stdlib><span style="color: #0000ff">int</span> main()
{
	<span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;

	ifstream inFile;

	inFile.open("<span style="color: #8b0000">my.dat</span>",ios::in);
	<span style="color: #0000ff">if</span>(!inFile)
	{
		cerr 不能打开my.dat" return 0;

}</stdlib></iostream></fstream>

运行结果:

未命名

分享到:
评论

相关推荐

    C++文件的使用

    只有当函数被调用时没有声明方式参数的情况下,默认值才会被采用。如果函数被调用时声明了任何参数,默认值将被完全改写,而不会与调用参数组合。 由 于对类ofstream, ifstream 和 fstream 的对象所进行的第一个操作...

    在C++中,使用文件流(std::ifstream 和 std::ofstream)可以方便地读取和写入文本文件 下面是一些示例

    在C++中,使用文件流(std::ifstream 和 std::ofstream)可以方便地读取和写入文本文件。下面是一些示例代码,展示了如何使用这些文件流来操作文本文件。 读取文本文件 cpp #include &lt;iostream&gt; #include ...

    ofstream和ifstream详细用法

    * `ios::nocreate`:不建立文件,所以文件不存在时打开失败 * `ios::noreplace`:不覆盖文件,所以打开文件时如果文件存在失败 * `ios::trunc`:如果文件存在,把文件长度设为 0 可以用“或”把以上属性连接起来,...

    c++文件读写.pdf

    * ios::nocreate:不建立文件,所以文件不存在时打开失败 * ios::noreplace:不覆盖文件,所以打开文件时如果文件存在失败 * ios::trunc:如果文件存在,把文件长度设为 0 可以用“或”把以上属性连接起来,如 ios...

    ifstream和getline读取文件

    ifstream和getline读取文件

    摄影测量空间后方交会

    ifstream filein(filename,ios::in|ios::nocreate); // 以输入方式打开一个文件,ios::nocreate表示原文件不存在时,不创建 if(!filein) { cout打开文件失败!文件可能不存在!"; exit(1); } for(int i=0;i;i...

    第八节 C++ 标准函数库.docx

    ifstream file("example.txt", ios::in); 5. 文件模式(File Modes) C++ 中的文件模式决定了文件的打开方式。常见的文件模式有: * ios::in:以输入(读)方式打开文件 * ios::out:以输出(写)方式打开文件 * ...

    有关c++训练的一些题目

    构造函数的名称与类名相同,并且没有返回值。 示例代码:CStudent::CStudent(char *name ,char * id,float score=0){...}; 3. 重载运算符:C++允许用户重载运算符,以满足特定的需求。常见的运算符重载有输入输出...

    C++文件操作详解[借鉴].pdf

    file.open("example.bin", ios::out | ios::app | ios::binary); 三、关闭文件 当文件读写操作完成后,需要关闭文件以使文件重新变为可访问的。可以使用close()成员函数关闭文件,该函数的格式为: void close();...

    C++文件处理 cpp

    C++ 文件处理详解 ...| ios::nocreate | 如果文件不存在则打开失败 | | ios::binary | 二进制文件(非文本文件) | 对于 ifstream 流,mode 的默认值为 ios::in ;对于 ofstream 流,mode 的默认值为 ios::out。

    stl库手册_c++.rar

    ifstream is; is.open ("test.txt", ios::binary ); // get length of file: is.seekg (0, ios::end); length = is.tellg(); is.seekg (0, ios::beg); // allocate memory: buffer = new char [length]; ...

    C++stl标准库手册

    ifstream is; is.open ("test.txt", ios::binary ); // get length of file: is.seekg (0, ios::end); length = is.tellg(); is.seekg (0, ios::beg); // allocate memory: buffer = new char [length]; ...

    哈弗曼编码

    哈弗曼 压缩 解压 对话框资源 ... file.open(name,ios::in|ios::binary); char ch; while(file.get(ch)) //每次取8位字节 { T[(int)ch+128].weight++; //将-127~+127 转换成 0~255 统计权值 } }

    C++ 二进制读写文件的方法

    ifstream file&#40;filePath.c_str(&#41;, std::ios::in | std::ios::binary); if(!(file.is_open())) { return; } file.seekg(0, std::ios::end); //定位输入流结束位置 std::ios::pos_type endPos = file....

    C++读写文本文件和二进制文件(源代码)

    对于二进制文件,我们同样使用std::ofstream和std::ifstream类,但设置了std::ios::binary标志以指定二进制模式,并使用write和read成员函数直接操作字节流。在二进制文件读取示例中,我们特别处理了文件结束和读取...

    c++输入文件流ifstream用法详解_ims的博客-CSDN博客1

    c++输入文件流ifstream用法详解_ims的博客-CSDN博客c++输入文件流ifstream用法详解原创阅读数 43446收藏发布于2018-05-18

    ifstream.pdf

    ifstream.pdf ifstream.pdf ifstream.pdf

    操作系统实验一-系统调用.doc

    ifstream in(argv[2], ios::binary | ios::in); //the file needed to copy is not exited if (!in) { cout [2] ; system("pause"); return -1; } ofstream out(argv[3], ios::binary | ios::out); char it...

    C++,java读写二进制文件方法介绍.docx

    * `ifstream` 是一个输入文件流,用于读取文件。 * `ofstream` 是一个输出文件流,用于写入文件。 * 使用文件流需要包含 `&lt;fstream.h&gt;` 头文件。 Java读写二进制文件 * 在Java中,读写二进制文件需要使用 `...

    ofstream和ifstream详细用法 .doc

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O,stream这个类有两个重要的运算符:

Global site tag (gtag.js) - Google Analytics