Click here to Skip to main content
11,710,423 members (87,437 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
We are opening a file in a.c and we are closing it .We have another file b.c in that file we have to check whether a file is closed/notclosed in a.c.
how to know that whether it is closed or not.

a.c
----
int main()
{
...
...
fp = fopen("abc.txt","rw");
fclose(fp);
..
...
}
 
b.c
----
if(fp == 0)
{
  printf("file closed successfully \n");
}
else
{
  printf("file not closed \n");
}
what kind of approach can be taken to solve this ?
Posted 3-Feb-13 19:11pm
Edited 4-Feb-13 2:06am
RyanDev168.4K
v3
Comments
Zoltán Zörgő at 4-Feb-13 8:09am
   
You can't check this precisely just with C language tools, you need operating system level support to get status of the file handles.
Marius Bancila at 7-Feb-13 9:04am
   
Notice that the file could be closed just the next moment after you've found the file was opened, or the other way around. Therefore make sure you're asking the right question when you code "is this file opened".
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If your operating system is Microsoft Windows, you can use the platform-specific Openfile function to open the file with the "Exclusive" attribute OF_SHARE_EXCLUSIVE. You will get an error result if another process has the file open.
  Permalink  
v6
Comments
Albert Holguin at 5-Feb-13 2:12am
   
to add... this does imply you have to use the platform specific calls (such as the WinAPI call OpenFile). +5
H.Brydon at 13-Feb-13 0:53am
   
Almost correct - if the file is open elsewhere you get a return code of HFILE_ERROR. No exception is thrown from OpenFile().
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The solutions so far assume your b.c is compiled into a separate executable or runs on a separate thread from a.c?
Anything that involves crossing threads or process will involve platform specific API calls because threads and processes are platform specific things.

However if a.c and b.c are part of the same executable and you're only running one thread then you simply set a flag in a.c

int iOpenFlag = 0;
 
fp = fopen("abc.txt","rw");
iOpenFlag = 1;
fclose(fp);
iOpenFlag = 0;
then in b.c
extern int iOpenFlag; //tells the linker to look for iOpenFlag from the other c files in the link

if( iOpenFlag == 0 )
{
  //file is closed
}
Clearly error and return value checking are needed to ensure the flag is only set and cleared when it should be.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 549
1 OriginalGriff 260
2 Peter Leow 134
3 Abhinav S 130
4 Richard MacCutchan 90
0 Abhinav S 60
1 Member 4653839 40
2 barneyman 35
3 Sergey Alexandrovich Kryukov 35
4 Member 11793820 35


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100