Solved

Auto detect a file

Posted on 2003-11-20
7
256 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 34 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 33 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Assisted Solution

by:Axter
Axter earned 33 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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