Solved

Auto detect a file

Posted on 2003-11-20
7
248 Views
Last Modified: 2010-04-01
There are 2 text files, 1 is named as output and the other is ok.
How do I write the codings in C++ if i want the following logic to be done stated below:

1. If ok file was not found, it will automatically writes to the output file.
2. If ok file was found, it would not automatically writes to the output file until the ok file was deleted.

Can anyone there help me?
0
Comment
Question by:icyelite
7 Comments
 
LVL 3

Expert Comment

by:freewell
Comment Utility
You don't need to specifically delete the output file. You just open the output file with create attribute and the file will be overwritten with new content.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 34 total points
Comment Utility
1.  The  fstat() function is a quick and easy way to see if a file already exists.

2) Do you want the program to wait around until somebody deletes the file, or do you want to delete it yourself, or what?

Is this for programming Windows?  If so, you can use  the
     FindFirstChangeNotification
API to get a motification when a file gets deleted.

-- Dan
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 33 total points
Comment Utility
If you are on UN*X, you might be interested in the native file locking; see the demonstration program at http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html .

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp if you are on Windoze.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 30

Assisted Solution

by:Axter
Axter earned 33 total points
Comment Utility
I recommend using stat.
With stat, you don't have to open the file.

Example code:

#include < sys/stat.h>
#include < io.h>

bool PathFileExists(const char* FileName)
{
   struct stat my_stat;
   return (stat(FileName, &my_stat) == 0);
}

bool IsDirectory(const char* FileName)
{
   struct stat my_stat;
   if (stat(FileName, &my_stat) != 0) return false;
   return ((my_stat.st_mode & S_IFDIR) != 0);
}

int main(int argc, char* argv[])
{
    bool ThisFileExist = PathFileExists("c:\\IO.SYS");
    bool ThisFileIsDir = IsDirectory("c:\\IO.SYS");
    return 0;
}
 
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Win32 code can also use PathFileExists API function.

This function has the following requirements:
Version 4.71 and later of Shlwapi.dll
Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later).
Windows 95/98: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later).
Header: Declared in shlwapi.h.
Import Library: shlwapi.lib.

If the older operating system does not have the latest update, this function may fail.


0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: DanRollins {http:#9792762} & rstaveley {http:#9794759} & Axter {http:#9796290}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now