473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ifstream: reopening a file after it was renamed/deleted

Hello,

I was looking for a solution on http://www.cplusplus.com/ and serched
groups, but cannot find anything interesting.

My program opens an input file stream:
ifstream f( "in.txt" );
then goes to its end:
f.seekg( 0, ios_base::end );
and then, in a loop, waits for a new line to be put into this file,
and reads it:
string line;
while( true )
{
f.clear();
if( f.peek() != EOF )
{
getline( f, line );
cout << "" << line << "\n";
}
usleep( 50000 );
}

However the file in.txt may be renamed or deleted, and a new, empty
one may be created in its place.
How can I detect it and close the current stream, and reopen it with
the new in.txt file?
Thank you.
Piotr D±browski
Mar 9 '08 #1
2 2180
Sam
ultr writes:
Hello,

I was looking for a solution on http://www.cplusplus.com/ and serched
groups, but cannot find anything interesting.

My program opens an input file stream:
ifstream f( "in.txt" );
then goes to its end:
f.seekg( 0, ios_base::end );
and then, in a loop, waits for a new line to be put into this file,
and reads it:
string line;
while( true )
{
f.clear();
if( f.peek() != EOF )
{
getline( f, line );
cout << "" << line << "\n";
}
usleep( 50000 );
}

However the file in.txt may be renamed or deleted, and a new, empty
one may be created in its place.
How can I detect it and close the current stream, and reopen it with
the new in.txt file?
That's not a part of the C++ language per se, but rather depends on your
platform. On POSIX platforms, for example, you would use the stat() call,
and keep track of st_dev, and st_ino.

Unfortunately, there are a number of race conditions here due to the fact
that ifstream does not let you access the underlying file descriptor, unless
your C++ library gives you its own, specific, methods for doing so. The
correct solution is to run fstat() on the open file descriptor, and stat()
on the filename, then compare the two.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBH1E4Mx9p3GYHlUOIRAlJGAJ0S0iYk/HCT6jBOAJlHd3S4IcAVugCdGbMI
tJmKaK4si6ZRsVXUiPV6Jk0=
=GI/W
-----END PGP SIGNATURE-----

Mar 9 '08 #2
On POSIX platforms, for example, you would use the stat() call,
and keep track of st_dev, and st_ino.
That's what I was looking for. It solves the problem.

Thanks.
Mar 9 '08 #3

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

Similar topics

4
by: siliconmike | last post by:
What would happen on Linux if: $F=fopen("hello","wb"); lock $F write to $F rename "hello" as "mango" close $F Will mango contain upto date data ? Or will the rename cause it to contain...
3
by: zfeld | last post by:
My app is creating a word document which my app calls saveas() and saves it to a given directory that is being watched by a fileSystemWatcher. The problem is that the fileSystemEventHandler for...
3
by: Ron Vecchi | last post by:
Hi all, Can someone point me in the direction on this one, resources or examples. I am writing a Windows Service to monitor a folder. When I file is modified within the folder or any subfolders...
5
by: Jon Maz | last post by:
Hi there, I am experimenting with the FileSystemWatcher object. I have set the NotifyFilter as follows: myFileSystemWatcher.NotifyFilter = NotifyFilters.Security | NotifyFilters.CreationTime...
1
by: apple | last post by:
i try to print image file in a directory using PrintDocument. It will raise printPage event to draw image to the printer. The file will be deleted after print and the directory will be checked...
6
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error...
9
by: Eli Luong | last post by:
Hi, I have the following code below. The input file is one number/line, but the while loop is only occuring once, and it's only taking in the value of the last number (count only goes to 1 and...
7
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm new to Visual Studio 2005. I'm creating a windows application using Visual Basic. After I added a control to a form and added some codings to the control, I want to rename the control. ...
6
by: Andy B | last post by:
What would i use to look for changes in a file every minute?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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,...

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.