ifstream: reopening a file after it was renamed/deleted

Discussion in 'C++' started by ultr, Mar 9, 2008.

  1. ultr

    ultr Guest

    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
     
    ultr, Mar 9, 2008
    #1
    1. Advertising

  2. ultr

    Sam Guest

    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-----
     
    Sam, Mar 9, 2008
    #2
    1. Advertising

  3. ultr

    ultr Guest

    > 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.
     
    ultr, Mar 9, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trint Smith
    Replies:
    2
    Views:
    731
    Ravi Sankar
    Aug 25, 2003
  2. Tomas Mikula
    Replies:
    7
    Views:
    431
    Andreas Leitgeb
    Nov 15, 2007
  3. Chumley the Walrus
    Replies:
    2
    Views:
    254
    Tom Gosselin
    Aug 10, 2004
  4. Amarpal
    Replies:
    2
    Views:
    869
    harsha
    Dec 31, 2011
  5. c676228

    file renamed after server upgrade

    c676228, Jan 25, 2008, in forum: ASP General
    Replies:
    19
    Views:
    228
    Steven Cheng[MSFT]
    Feb 7, 2008
Loading...

Share This Page