473,499 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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.

Nov 28 '07 #1
4 1197
On Nov 28, 11:35 am, Ramon F Herrera <ra...@conexus.netwrote:
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

Nov 28 '07 #2
Ramon F Herrera wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 28 '07 #3
On Nov 28, 11:38 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Ramon F Herrera wrote:
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?
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
Nov 28 '07 #4
Ramon F Herrera wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1209
by: Dave Benjamin | last post by:
The recent conversation on prototype-based OOP and the Prothon project has been interesting. I've been playing around with the idea for awhile now, actually, since I do a lot of programming in...
31
14283
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
10
1381
by: sam | last post by:
hi, i am new to C/C++ and have just finished a program based on the UK national lottery, where you enter 6 numbers, six are generated by the computer and there are appropriate messages and a...
1
1142
by: Robin | last post by:
H My worthless textbook gives an example where you create a structure to mimic a Random Access file layout and you are expected to use an instantiation of that to pull data out of the Random...
30
1785
by: Brian Elmegaard | last post by:
Hi, I am struggling to understand how to really appreciate object orientation. I guess these are FAQ's but I have not been able to find the answers. Maybe my problem is that my style and...
20
1804
by: ilya2 | last post by:
I am supposed to teach an introductory C course with an unusual slant, and have trouble finding an appropriate textbook. The course will begin traditionally enough with variables, loops,...
1
1172
by: cookieplanter | last post by:
I need to add content to two DIVs on page load but this just won't work. If I call any one function, things work fine or if I trigger the events on-click, it works perfectly. But with both together...
176
8184
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
6
2306
by: beantaxi | last post by:
Hello all, I'm looking for a very simple code analysis tool. I have a large codebase to analyze, and all I really need to do is to find all uses of all methods in a few interfaces. Many tools...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.