<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>
运行结果:
分享到:
相关推荐
只有当函数被调用时没有声明方式参数的情况下,默认值才会被采用。如果函数被调用时声明了任何参数,默认值将被完全改写,而不会与调用参数组合。 由 于对类ofstream, ifstream 和 fstream 的对象所进行的第一个操作...
在C++中,使用文件流(std::ifstream 和 std::ofstream)可以方便地读取和写入文本文件。下面是一些示例代码,展示了如何使用这些文件流来操作文本文件。 读取文本文件 cpp #include <iostream> #include ...
ifstream和getline读取文件
ifstream filein(filename,ios::in|ios::nocreate); // 以输入方式打开一个文件,ios::nocreate表示原文件不存在时,不创建 if(!filein) { cout打开文件失败!文件可能不存在!"; exit(1); } for(int i=0;i;i...
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]; ...
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 统计权值 } }
ifstream file(filePath.c_str(), std::ios::in | std::ios::binary); if(!(file.is_open())) { return; } file.seekg(0, std::ios::end); //定位输入流结束位置 std::ios::pos_type endPos = file....
对于二进制文件,我们同样使用std::ofstream和std::ifstream类,但设置了std::ios::binary标志以指定二进制模式,并使用write和read成员函数直接操作字节流。在二进制文件读取示例中,我们特别处理了文件结束和读取...
ifstream.pdf ifstream.pdf ifstream.pdf
c++输入文件流ifstream用法详解_ims的博客-CSDN博客c++输入文件流ifstream用法详解原创阅读数 43446收藏发布于2018-05-18
ofstream和ifstream详细用法总结
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O,stream这个类有两个重要的运算符:
ifstream infile("statics.dat",ios::in); if(!infile) { cerr!"; exit(1); } infile>>c.tcount; infile>>c.scount; infile.close(); return (c); } // void person::show()const // 输出person { cout...
fstream fin(a.txt,ios::in); if(fin) { cout<<opened<<endl; fin.close(); } else { cout<<not exists<<endl; } 注意:如果文件不存在,则打开失败。如果这里打开方式采用的是ios::out...
C++获取文件大小C++获取文件大小利用ifstream利用C函数利用Windows _stat函数参考文章 C++获取文件大小 利用ifstream 以下操作需要包含头文件fstream 和 string size_t GetFileSize(const std::string& file_name){...
ifstream infile("student.txt",ios::in|ios::binary); if(!infile) { cerr!"; exit(1); } infile.read((char *)&stud,sizeof(stud)); infile.close(); } void Student::save() { Student stud; stud.read...
使用ifstream和getline读取文件内容[c++],简单地介绍了一下如何配合使用两者来读取文件内容。
利用ifstream和ofstream写的文件分割和合并 用c++中的纯fstream写的文件分割和合并程序, 大家可以看看,加强一下c++中的文件操作
ofstream ifstream 文件操作 是VC中的文件读入读出的基本操作!