Solved

Auto detect a file

Posted on 2003-11-20
7
251 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help with listview limit text 20 103
How to copy an image file into clipboard C/C++? 1 168
gdb doesn't stop on breakpoint 2 67
Beginner to Unreal Engine 4 5 79
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…
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

785 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