由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Why no output file generate? What is wrong?
相关主题
请问一个入门级 dynamic memory 的问题无法编译一个文件
What is wrong with the code?C++默认的copy constructor的疑惑
一个极简单的程序求教a simple question for C++ class
fstream 扫盲,谢谢!请问一个exception题目
C++: Static initialization dependency关于 VC++ vitual, reload 和 derive的一个问题...
C++ problem发个初级面试题
how to destruct list with loop?最初级的白痴C++问题
读取数据求教两个继承问题
相关话题的讨论汇总
话题: fout话题: fstream话题: what话题: why话题: file
进入Programming版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
Why output.txt not generated? What is wrong?
#include
#include
using namespace std;
void print(int num, fstream& fout)
{ fout << num << endl; }
int main() {
fstream fout;
fout.open("output.txt");
print(2, fout);
print(3, fout);
}
S*********g
发帖数: 5298
2
fout.close();

【在 c**********e 的大作中提到】
: Why output.txt not generated? What is wrong?
: #include
: #include
: using namespace std;
: void print(int num, fstream& fout)
: { fout << num << endl; }
: int main() {
: fstream fout;
: fout.open("output.txt");
: print(2, fout);

t****t
发帖数: 6806
3
since you used fstream, the open mode is 0 by default. you have to
explicitly say
fout.open("...", ios_base::out);
or alternatively, use
ofstream fout;
fout.open("...");
EDIT: the default open mode is in|out by default. seems it's not stated
on standard, but i guess most implementation will do that. however, if
you specify ios_base::in, and file do not exist, the open will fail anyway.

【在 c**********e 的大作中提到】
: Why output.txt not generated? What is wrong?
: #include
: #include
: using namespace std;
: void print(int num, fstream& fout)
: { fout << num << endl; }
: int main() {
: fstream fout;
: fout.open("output.txt");
: print(2, fout);

t****t
发帖数: 6806
4
this is optional given fstream is auto variable and will be destructed
automatically.

【在 S*********g 的大作中提到】
: fout.close();
c**********e
发帖数: 2007
5
Thanks, guru. It works.

【在 t****t 的大作中提到】
: since you used fstream, the open mode is 0 by default. you have to
: explicitly say
: fout.open("...", ios_base::out);
: or alternatively, use
: ofstream fout;
: fout.open("...");
: EDIT: the default open mode is in|out by default. seems it's not stated
: on standard, but i guess most implementation will do that. however, if
: you specify ios_base::in, and file do not exist, the open will fail anyway.

P********e
发帖数: 2610
6
r u sure

this is optional given fstream is auto variable and will be destructed
automatically.

【在 t****t 的大作中提到】
: this is optional given fstream is auto variable and will be destructed
: automatically.

t****t
发帖数: 6806
7
go ahead and do your test if you don't believe it. or read the spec.

【在 P********e 的大作中提到】
: r u sure
:
: this is optional given fstream is auto variable and will be destructed
: automatically.

P********e
发帖数: 2610
8
u r right
but, it's better to have a close
如果是c的话
没有close (fp)是有问题的

【在 t****t 的大作中提到】
: go ahead and do your test if you don't believe it. or read the spec.
t****t
发帖数: 6806
9
sure it's a good habit to have a close
but for the purpose of debugging, it's optional

【在 P********e 的大作中提到】
: u r right
: but, it's better to have a close
: 如果是c的话
: 没有close (fp)是有问题的

t****t
发帖数: 6806
10
btw, for C, on exit of process, close() is optional, fclose() is mandatory
file descriptors are always automatically closed on exit, but the buffer
provided by FILE* is not automatically flushed
of course, again it's a good habit to close()/fclose() whenever the fd/FILE*
is no longer needed

【在 P********e 的大作中提到】
: u r right
: but, it's better to have a close
: 如果是c的话
: 没有close (fp)是有问题的

E*V
发帖数: 17544
11
en. in when the program terninated normally

【在 P********e 的大作中提到】
: r u sure
:
: this is optional given fstream is auto variable and will be destructed
: automatically.

E*V
发帖数: 17544
12
ofstream is better a

【在 c**********e 的大作中提到】
: Why output.txt not generated? What is wrong?
: #include
: #include
: using namespace std;
: void print(int num, fstream& fout)
: { fout << num << endl; }
: int main() {
: fstream fout;
: fout.open("output.txt");
: print(2, fout);

1 (共1页)
进入Programming版参与讨论
相关主题
两个继承问题C++: Static initialization dependency
为什么我看不懂下面的code,是不是水平还不够?C++ problem
g++-2.95 -> g++-3.3/3.4how to destruct list with loop?
请教函数 INIT 怎么能free memory读取数据求教
请问一个入门级 dynamic memory 的问题无法编译一个文件
What is wrong with the code?C++默认的copy constructor的疑惑
一个极简单的程序求教a simple question for C++ class
fstream 扫盲,谢谢!请问一个exception题目
相关话题的讨论汇总
话题: fout话题: fstream话题: what话题: why话题: file