| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2144 人关注过本帖
标题:[求助]为什么编译通不过
只看楼主 加入收藏
chenkuanyi
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-4-21
收藏
 问题点数:0 回复次数:2 
[求助]为什么编译通不过

#include <iostream>
#include <fstream>
using namespace std;
void main(void)
{
ifstream infile("myfile.txt",ios::in|ios::nocreate);
if (!infile)
{
cout<<"文件不存了,不能打开的文件:"<<"myfile.txt"<<endl;
exit(1);
}
}
myfile.txt 是存在的

38.cpp(6) : error C2039: “nocreate”: 不是“std::basic_ios<_Elem,_Traits>”的成

with
[
_Elem=char,
_Traits=std::char_traits<char>
]
38.cpp(6) : error C2065: “nocreate”: 未声明的标识符

为什么啊,还要加什么头文件不成!

[此贴子已经被作者于2007-5-4 8:41:02编辑过]

搜索更多相关主题的帖子: ios std txt 编译 
2007-05-04 08:38
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

#include <iostream>
#include <fstream>
using namespace std;
void main(void)
{
ifstream infile("myfile.txt",ios::in|ios::nocreate);
if (!infile)
{
cout<<"文件不存了,不能打开的文件:"<<"myfile.txt"<<endl;
exit(1);
}
}
myfile.txt 是存在的

38.cpp(6) : error C2039: “nocreate”: 不是“std::basic_ios<_Elem,_Traits>”的成

with
[
_Elem=char,
_Traits=std::char_traits<char>
]
38.cpp(6) : error C2065: “nocreate”: 未声明的标识符

为什么啊,还要加什么头文件不成!


ios::nocreate是在C++标准制定之前在<fstream.h>中有定义的。但是因为它跟系统平台相关密切,所以在C++标准中去掉了对它的支持。可以先以只读方式打开文件,判断文件的存在性(即文件是否打开成功)。如果文件不存在,什么也不做;如果文件存在,则关闭文件,然后以写方式打开文件。这样就实现了ios::nocreate表示的功能。

[此贴子已经被作者于2007-5-4 10:16:01编辑过]


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-05-04 10:00
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

fstream fs(“fname”, ios_base::in);       // attempt open for readif
(!fs) 
{
        // file doesn't exist; don't create a new one
}else
//ok, file exists. close and reopen in write mode
{
fs.close();
fs.open(“fname”, ios_base::out);         // reopen for write
}

You can just do the opposite for ios::noreplace:

fstream fs(“fname”, ios_base::in);// attempt open for readif
(!fs)
{
   // file doesn't exist; create a new one fs.open(“fname”, ios_base::out);
}
else       //ok, file exists; close and reopen in write mode
{
fs.close() fs.open(“fname”, ios_base::out);       // reopen for write
}


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-05-04 10:43
快速回复:[求助]为什么编译通不过
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011932 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved