ofstream的 [英] ofstream
问题描述
你好,
ofstream ofs;
fun(char * str)
{
ofs<< str<< endl;
}
void main()
{
int i = 0;
....
....
if(i)
{
ofs.open(" text.txt");
fun(" heih0") ;
}
其他
{
//这里我不打开文件
fun(heih0);
}
}
在上面的代码中,在其他地方条件iam没有打开文件,但仍然是
调用fun(),这里发生了什么???
问候
Hello,
ofstream ofs;
fun(char* str)
{
ofs<<str<<endl;
}
void main()
{
int i=0;
....
....
if(i)
{
ofs.open("text.txt");
fun("heih0");
}
else
{
// Here iam not opening file
fun("heih0");
}
}
In the above code, in else condition iam not opening file, but still
calling fun(), What happens here???
Regards
推荐答案
" Gurikar" < MS ******* @ gmail.com>在消息中写道
news:11 ********************** @ g43g2000cwa.googlegr oups.com
"Gurikar" <ms*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com
你好,
ofstream ofs;
有趣(char * str)
{
ofs<< str<< endl;
}
void main()
{i />
int i = 0;
...
...
if(i)
{
ofs.open(" text.txt");
fun(" heih0");
}
否则
//这里我不打开文件
有趣(heih0);
}
}
>在上面的代码中,在其他情况下iam没有打开文件,但仍然在调用fun(),这里发生了什么???
问候
Hello,
ofstream ofs;
fun(char* str)
{
ofs<<str<<endl;
}
void main()
{
int i=0;
...
...
if(i)
{
ofs.open("text.txt");
fun("heih0");
}
else
{
// Here iam not opening file
fun("heih0");
}
}
In the above code, in else condition iam not opening file, but still
calling fun(), What happens here???
Regards
我在这里,用锤子敲打我的头。这里会发生什么?
-
John Carson
Here I am, banging my head with a hammer. What happens here?
--
John Carson
Gurikar < MS ******* @ gmail.com>在消息中写道
news:11 ********************** @ g43g2000cwa.googlegr oups.com ...
"Gurikar" <ms*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
你好,
ofstream;
有趣(char * str)
{
ofs<<< str<< endl;
}
void main()
{i / 0
int i = 0;
...
...
如果(i)
{
ofs.open(" text.txt");
有趣(" heih0");
}
{
//这里我没有打开文件
有趣(heih0);
}
}
在上面的代码中,在其他情况下iam没有打开文件,但仍然在调用fun(),这里会发生什么???
Hello,
ofstream ofs;
fun(char* str)
{
ofs<<str<<endl;
}
void main()
{
int i=0;
...
...
if(i)
{
ofs.open("text.txt");
fun("heih0");
}
else
{
// Here iam not opening file
fun("heih0");
}
}
In the above code, in else condition iam not opening file, but still
calling fun(), What happens here???
谁知道,上面的代码不是C ++。也不会在任何
编译器(c ++或不是c ++)上编译。 fun()没有返回类型,fun()中的ofs未定义。
main必须返回一个整数。你需要阅读
对象的范围和生命周期。
尝试类似:
#include < iostream>
#include< fstream>
#include< string>
void fun(std :: ofstream& ofs,const std :: string s)
{
ofs<< s<< std :: endl;
}
int main()
{
bool b_havefun = false;
//输出文件流
std :: string s_file(" text.txt");
std :: ofstream ofs;
ofs.open(s_file.c_str());
if(!ofs)
{
std :: cout<< 打开时出错 << s_file<< std :: endl;
}
if(b_havefun)
{
fun(ofs) ,让派对);
}
其他
{
fun(ofs,off工作);
}
返回0;
}
写保护text.txt文件以查看错误消息。
Who knows, the above code is not C++. Neither would it compile on any
compiler(c++ or not). fun() has no return type, ofs in fun() is not defined.
main must return an integer. You need to read up on scopes and lifetime of
objects.
try something like:
#include <iostream>
#include <fstream>
#include <string>
void fun(std::ofstream& ofs, const std::string s)
{
ofs << s << std::endl;
}
int main()
{
bool b_havefun = false;
// output file stream
std::string s_file("text.txt");
std::ofstream ofs;
ofs.open(s_file.c_str());
if (!ofs)
{
std::cout << "error while opening " << s_file << std::endl;
}
if (b_havefun)
{
fun(ofs, "lets party");
}
else
{
fun(ofs, "off to work");
}
return 0;
}
Write-protect the text.txt file to see the error message.
" Gurikar" < MS ******* @ gmail.com>在消息中写道
news:11 ********************** @ g43g2000cwa.googlegr oups.com ...
"Gurikar" <ms*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
你好,
#include< cstdlib>
#include< fstream>
#include< ostream>
使用命名空间std;
ofstream ofs;
有趣(char * str)
需要返回类型。例如:
void fun(char * str)
{
ofs<< str<< endl;
}
void main()
''main()''的返回类型为''int''。
>
int main()
{
int i = 0;
...
...
if(i)
{
ofs.open(" text.txt");
您应该检查开放是成功还是失败。
if(!ofs)
{
cerr<< 不能打开文件\ n;;
返回EXIT_FAILURE;
}
fun(" heih0" ;);
}
{
//这里我没有打开文件
有趣(heih0);
}
}
在上面的代码中,在其他情况下iam没有打开文件,但仍然是
调用fun(),这里发生了什么???
Hello,
#include <cstdlib>
#include <fstream>
#include <ostream>
using namespace std;
ofstream ofs;
fun(char* str)
Return type required. e.g.:
void fun(char *str)
{
ofs<<str<<endl;
}
void main()
''main()'' is required to have return type of ''int''.
int main()
{
int i=0;
...
...
if(i)
{
ofs.open("text.txt");
You should check whether the open succeeded or failed.
if(!ofs)
{
cerr << "can''t open file\n";
return EXIT_FAILURE;
}
fun("heih0");
}
else
{
// Here iam not opening file
fun("heih0");
}
}
In the above code, in else condition iam not opening file, but still
calling fun(), What happens here???
>
调用ofs<<将失败。
-Mike
The call to ofs<< will fail.
-Mike
这篇关于ofstream的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!