473,699 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finish a file

how the program knows when a file is finsihed, mmm?

Nov 12 '06
18 1793
Roland Pibinger wrote:
On Tue, 14 Nov 2006 04:35:11 -0800, "Jim Langston" wrote:
"Roland Pibinger" wrote in message
After close() check the stream state. Otherwise you don't know "when a
file is finsihed".
Please explain this. After you call .close() don't we know that the file is
"finished"?

But when .close() fails?
Can .close() fail? I think not.

Nov 15 '06 #11
Daniel T. wrote:
Roland Pibinger wrote:
>On Tue, 14 Nov 2006 04:35:11 -0800, "Jim Langston" wrote:
>"Roland Pibinger" wrote in message
After close() check the stream state. Otherwise you don't know "when a
file is finsihed".

Please explain this. After you call .close() don't we know that the
file is "finished"?

But when .close() fails?

Can .close() fail? I think not.
The standard seems to allow close to fail [27.8.1.3/6]:

basic_filebuf<c harT,traits>* close();

Effects: If is_open() == false, returns a null pointer. If a put area
exists, calls overflow(EOF) to flush characters. If the last virtual
member function called on *this (between underflow, overflow, seekoff,
and seekpos) was overflow then calls a_codecvt.unshi ft (possibly several
times) to determine a termination sequence, inserts those characters and
calls overflow(EOF) again. Finally it closes the file (??as if?? by
calling std::fclose(fil e)). If any of the calls to overflow or
std::fclose fails then close fails.

Returns: this on success, a null pointer otherwise.

Postcondition: is_open() == false.
Best

Kai-Uwe Bux
Nov 15 '06 #12
Kai-Uwe Bux <jk********@gmx .netwrote:
Daniel T. wrote:
>Roland Pibinger wrote:
>>On Tue, 14 Nov 2006 04:35:11 -0800, "Jim Langston" wrote:
"Roland Pibinger" wrote in message
>>>>After close() check the stream state. Otherwise you don't know
"when a file is finsihed".

Please explain this. After you call .close() don't we know that
the file is "finished"?

But when .close() fails?

Can .close() fail? I think not.

The standard seems to allow close to fail [27.8.1.3/6]:

basic_filebuf<c harT,traits>* close();

Effects: If is_open() == false, returns a null pointer. If a put area
exists, calls overflow(EOF) to flush characters. If the last virtual
member function called on *this (between underflow, overflow, seekoff,
and seekpos) was overflow then calls a_codecvt.unshi ft (possibly several
times) to determine a termination sequence, inserts those characters and

calls overflow(EOF) again. Finally it closes the file (??as if?? by
calling std::fclose(fil e)). If any of the calls to overflow or
std::fclose fails then close fails.

Returns: this on success, a null pointer otherwise.

Postcondition: is_open() == false.
So is_open() will subsequently and invariantly return false if close()
fails. Also, if close() returns a null pointer, that doesn't necessarily
mean that it failed (after all, it returns NULL if is_open() was false
on entry.

Quite a pickle. close() can "fail" but there is no way to know if it
actually did fail unless one first queries "is_open()" . Also, if it does
fail, we are left with no options because despite that failure
"is_open()" will still equal false, and thus subsequent calls to close()
will continue to return NULL.

So what is the robust way to close a file?

if ( file.is_open() ) {
if ( file.close() == 0 ) {
// what do we do here? [A]
}
}

I guess in [A] above, we could try to re-open the file, if that failed
then the file object becomes useless and we are done with it, if it
succeeds, we can try to close it again. Then we are stuck alternating
between opening the file and closing it again until such time as either
open fails, or close succeeds. A rather non-deterministic problem, the
program could get locked in a infinite loop.
if ( file.is_open() ) {
while ( file.close == 0 ) {
file.open();
if ( ! file.is_open() ) {
break;
}
}
}

Something like that? :-(

--
To send me email, put "sheltie" in the subject.
Nov 15 '06 #13
On Wed, 15 Nov 2006 03:21:46 GMT, "Daniel T." wrote:
>So what is the robust way to close a file?

if ( file.is_open() ) {
if ( file.close() == 0 ) {
..close() returns void
// what do we do here? [A]
}
}
What about:

file.close();
if (file.fail()) { // or file.bad()
// error
}

Better avoid iostreams for real work.

Best wishes,
Roland Pibinger
Nov 15 '06 #14
Roland Pibinger wrote:
Better avoid iostreams for real work.
Sorry, what?
Nov 15 '06 #15
In article <45************ *@news.utanet.a t>,
rp*****@yahoo.c om (Roland Pibinger) wrote:
On Wed, 15 Nov 2006 03:21:46 GMT, "Daniel T." wrote:
So what is the robust way to close a file?

if ( file.is_open() ) {
if ( file.close() == 0 ) {

.close() returns void
Kai-Uwe Bux quoted the standard saying that close() returns either
'this' or NULL.

So which is it?

--
To send me email, put "sheltie" in the subject.
Nov 15 '06 #16
Daniel T. wrote:
In article <45************ *@news.utanet.a t>,
rp*****@yahoo.c om (Roland Pibinger) wrote:
>On Wed, 15 Nov 2006 03:21:46 GMT, "Daniel T." wrote:
>So what is the robust way to close a file?

if ( file.is_open() ) {
if ( file.close() == 0 ) {

.close() returns void

Kai-Uwe Bux quoted the standard saying that close() returns either
'this' or NULL.
I quoted the close() method for the underlying buffer. The file stream has a
close() method, which calls the close() for the underlying buffer
[27.8.1.13/4]:

void close();
Effects: Calls rdbuf()->close() and, if that function returns false, calls
setstate(failbi t)(27.4.4.3) (which may throw ios_base::failu re).

As you can see, this one returns void but sets the failbit if it fails.

So which is it?
For the one from your code, void. Sorry for the confusion.
Best

Kai-Uwe Bux
Nov 15 '06 #17
Kai-Uwe Bux <jk********@gmx .netwrote:
Daniel T. wrote:
rp*****@yahoo.c om (Roland Pibinger) wrote:
On Wed, 15 Nov 2006 03:21:46 GMT, "Daniel T." wrote:
So what is the robust way to close a file?

if ( file.is_open() ) {
if ( file.close() == 0 ) {

.close() returns void
Kai-Uwe Bux quoted the standard saying that close() returns either
'this' or NULL.

I quoted the close() method for the underlying buffer. The file stream has a
close() method, which calls the close() for the underlying buffer
[27.8.1.13/4]:

void close();
Effects: Calls rdbuf()->close() and, if that function returns false, calls
setstate(failbi t)(27.4.4.3) (which may throw ios_base::failu re).

As you can see, this one returns void but sets the failbit if it fails.
But the function doesn't return a bool it returns a pointer or NULL. So
by "returns false" I can only assume that they mean "returns NULL".
Except that doesn't work either because close() returns NULL if the file
wasn't initially opened, thus NULL doesn't mean a failure to close in
that case. Or is my snippet above in the fstream's close() function and
it sets failbit if the file was both open and close returned NULL?

I'm still left wondering, what is one supposed to do with a file if
close fails?

--
To send me email, put "sheltie" in the subject.
Nov 15 '06 #18
"Daniel T." <da******@earth link.netwrote in message
news:da******** *************** *****@news.west .earthlink.net. ..
Kai-Uwe Bux <jk********@gmx .netwrote:
>Daniel T. wrote:
rp*****@yahoo.c om (Roland Pibinger) wrote:

On Wed, 15 Nov 2006 03:21:46 GMT, "Daniel T." wrote:
So what is the robust way to close a file?

if ( file.is_open() ) {
if ( file.close() == 0 ) {

.close() returns void

Kai-Uwe Bux quoted the standard saying that close() returns either
'this' or NULL.

I quoted the close() method for the underlying buffer. The file stream
has a
close() method, which calls the close() for the underlying buffer
[27.8.1.13/4]:

void close();
Effects: Calls rdbuf()->close() and, if that function returns false,
calls
setstate(failbi t)(27.4.4.3) (which may throw ios_base::failu re).

As you can see, this one returns void but sets the failbit if it fails.

But the function doesn't return a bool it returns a pointer or NULL. So
by "returns false" I can only assume that they mean "returns NULL".
Except that doesn't work either because close() returns NULL if the file
wasn't initially opened, thus NULL doesn't mean a failure to close in
that case. Or is my snippet above in the fstream's close() function and
it sets failbit if the file was both open and close returned NULL?

I'm still left wondering, what is one supposed to do with a file if
close fails?
I would think the only reason close would fail is if the file wasn't open in
the first place. And if it wasn't opened in the first place, then it is
closed. Consider close may fail in ths case:

std::ifstream MyFile( "Test.txt" );
if ( MyFile.is_open( ) )
{
// blah blah
}
MyFile.close();

If it wasn't able to open MyFile (it didn't exist, whatever) I presume that
the .close() would fail, but realisitcally, who cares? I already did a test
to see if it was open and if I needed something specific to do if it wasn't
found then I would have had an else. In practice it probably makes more
sense to put the .close() inside the if block, but I tend to put it outside
the block just like that, so I know when I'm done with a file without
looking inside blocks.

What does the standard say about the close() method itself, under what
conditions is it allowed to fail?
Nov 15 '06 #19

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

Similar topics

5
6301
by: Boris Nikolaevich | last post by:
This is backwards of what I usually want--normally if you have a long-running ASP script, it's a good idea to check to see whether the client is still connected so you can cancel execution. However, I have a script that absolutely MUST finish one it's been started--is there a way to cause the entire script to execute, even if the client disconnects in the middle of the process? It doesn't matter if the script returns anything to the...
39
789
by: jabailo | last post by:
I am looping through a text file, and with each row, I launch a web service, asynchronously. Before I move on to the next step in the process, I want to make sure that all the web services have completed. How can I do this? If I were to just put a Thread.Sleep with some arbitrary number ( say 5 minutes ) would the asynch web services continue to process? Or would they
11
20355
by: Peter Kirk | last post by:
Hi there I am looking at using a thread-pool, for example one written by Jon Skeet (http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if this pool provides the possibility to wait for all its threads to finish? For example, if I start 20 threads: CustomThreadPool pool = new CustomThreadPool("PetersThreadPool"); ThreadMethod m = new ThreadMethod(InsertThread);
6
3010
by: theGreatGnu | last post by:
Hi all, I am new to PHP, Apache and Mysql. But I least I have managed to install everything on my home computer on my own and everything is up and running. I can fetch data from the database and display it. So far, so good. However, I'm stuck on an irritating problem. Whenever I connect to mysql, my PHP pages don't want to finish loading. In the following example, the page will not finish loading nor display "abc" until I
4
8410
by: Dylan Parry | last post by:
Hi folks, I'm writing a program that needs to execute an external program and wait for it to finish running before it can make use of the output from that program. Specifically, the external program converts from one file format to another, and is something that I can't do natively :( So what I need to figure out is how to: a) Start the external program, with parameters
0
3810
by: mamod20 | last post by:
Please advise, I have the following example and want to know the best way to use $dbh->disconnect; and $sth->finish; -------------- $sql_host="localhost"; $sql_dataname = "database"; $sql_userid = "root"; $sql_password = "password"; &connect_sql;
1
2400
by: Craig Coope | last post by:
I may be barking up the wrong tree here (maybe I can do all this in Excel) but... I have created an Excel sheet that lets me input job start and finish times and the amount of work done within the jobs so that I can work out "total work per hour" etc....it works very well but I have to input the times and workload manually... Now is there anyway Excel or Excel linked with Access can be used so that for example, someone comes along and...
5
6159
by: Jim | last post by:
I have a form in an Access 2003 db that calls (using shell - thanks to this newsgroup) a Vb.net program I wrote to parse a text file. The call works fine. The VB parses the file correctly. How do I get the Access form to wait for the VB program to finish? Can the vb pass code back to Access? Can Access check somehow to see if VB is closed? I'm a VB.net newbie.
3
1255
by: Andy B | last post by:
I have the property below. What I need to do is access the asp.net Menu controls MenuItems in a content page that are on a master page. I need to set the Enabled state to false when the page loads and re enable it when the page goes to the next one. Any ideas how to finish this one? Or if I got it wrong? //return the main menu so other pages can use it. public Menu Menu {
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.