Solved

Auto detect a file

Posted on 2003-11-20
7
259 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

617 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