Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto detect a file

Posted on 2003-11-20
7
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:freewell
ID: 9792749
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 136 total points
ID: 9792762
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 132 total points
ID: 9794759
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Assisted Solution

by:Axter
Axter earned 132 total points
ID: 9796290
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
ID: 9796294
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
ID: 10249340
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 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