Simple textbook example won't work: bug or feature?

R

Ramon F Herrera

I am running the examples included in the book "Thinking in C++" (*).

This one has a a problem:

//: C02:Scopy.cpp
// Copy one file to another, a line at a time
#include <string>
#include <fstream>
using namespace std;

int main() {
ifstream in("Scopy.cpp"); // Open for reading
ofstream out("Scopy2.cpp"); // Open for writing
string s;
while(getline(in, s)) // Discards newline char
out << s << "\n"; // ... must add it back
} ///:~

The version above works fine, creating a copy of its own source code.
However, if I add this

exit (0);

statement at the end, an empty file is created instead.

Is it supposed to work that way?

-Ramon

(*) highly recommended, available for free.
 
R

Ramon F Herrera

I am running the examples included in the book "Thinking in C++" (*).

This one has a a problem:

//: C02:Scopy.cpp
// Copy one file to another, a line at a time
#include <string>
#include <fstream>
using namespace std;

int main() {
ifstream in("Scopy.cpp"); // Open for reading
ofstream out("Scopy2.cpp"); // Open for writing
string s;
while(getline(in, s)) // Discards newline char
out << s << "\n"; // ... must add it back
} ///:~

The version above works fine, creating a copy of its own source code.
However, if I add this

exit (0);

statement at the end, an empty file is created instead.

Is it supposed to work that way?

-Ramon

(*) highly recommended, available for free.

I ran the test under g++ 3.4.4 on Linux.

-RFH
 
V

Victor Bazarov

Ramon said:
I am running the examples included in the book "Thinking in C++" (*).

This one has a a problem:

//: C02:Scopy.cpp
// Copy one file to another, a line at a time
#include <string>
#include <fstream>
using namespace std;

int main() {
ifstream in("Scopy.cpp"); // Open for reading
ofstream out("Scopy2.cpp"); // Open for writing
string s;
while(getline(in, s)) // Discards newline char
out << s << "\n"; // ... must add it back
} ///:~

The version above works fine, creating a copy of its own source code.
However, if I add this

exit (0);

statement at the end, an empty file is created instead.

Is it supposed to work that way?

If you add 'in.close(); out.close()' right before calling 'exit',
what happens? If you instead of outputting "\n", output 'endl',
what changes?

Think about it, read the FM about the 'exit' function. What does
it say about destructors of the local objects?

V
 
R

Ramon F Herrera

If you add 'in.close(); out.close()' right before calling 'exit',
what happens?

It works as expected.

If you instead of outputting "\n", output 'endl',
what changes?

That works, too.

So the verdict is: feature.

Note to self: "do not modify textbook examples anymore".

Thanks,

-Ramon
 
V

Victor Bazarov

Ramon said:
[..]
Note to self: "do not modify textbook examples anymore".

You're drawing a completely opposite conclusion from what I'd
expect you to. Do modify those, do experiment, do ask questions
when you don't understand something. Do get to the bottom of
things, like you have here. You got your answer: "a feature".
That's great! Now, do you know why it is as it is? Have you
found the reason for 'exit' to change the behaviour of your
working program?

Perhaps using 'exit' is not the best approach here. After all
the 'main' function is actually expected to "return" the code
to the system. 'return 0;' is better than 'exit(0);', but you
need to find out why it is.

You will learn much more if you engage your brain by means of
actively participating in your learning. Just don't stop and
don't turn around at the very first tiny bump in the road.

Good luck!

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top