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);
|