The fwrite function is not returning error, if the file was removed.


 
Thread Tools Search this Thread
Top Forums Programming The fwrite function is not returning error, if the file was removed.
# 1  
Old 07-11-2013
Linux The fwrite function is not returning error, if the file was removed.

The fwrite function call is not returning error, when the file it writes to is removed, please advise on how to find if the file already opened and being written by a program is removed manually or by some other process.

please see the code below,

Code:
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
  int cnt = 10;
  int index;
  int count;
  FILE *fp;
  char message[50] = "writing to example file";

  fp = fopen("example","a");

  if (fp == NULL)
  {
    printf("opening file example failed\n");
    exit(01);
  }
  for (index = 0;index <= cnt;index++)
  {

    count = fwrite(message,sizeof(message),1,fp);
    fflush(fp);
    if (count != 1)
    {
      printf("Error writing to file example\n");
    }
    else
    {
      printf("data written to file example successfully\n");
    }
    sleep(20);
  }
  fclose(fp);
  return(00);
}

the above code was executed, I just left the file alone during the first two loops and then removed the file during the sleep and after that as well, the program is printing the success message, even though the file is not available.

Please advise.Smilie
# 2  
Old 07-11-2013
The file no longer has any links in the filesystem, but the file still exists until your application closes the file. (Note that exiting the process closes the file even if you don't explicitly call fclose().) You can see when this happens by checking the link count on the file (as shown in the following example). Obviously, you should add error checking to the fstat() call, but this should work for demonstration purposes:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main(void) {
        int             cnt = 10;
        int             count;
        int             index;
        FILE            *fp;
        char            message[50] = "writing to example file";
        struct stat     sbuf;

        fp = fopen("example","a");

        if (fp == NULL) {
                printf("opening file example failed\n");
                exit(01);
        }
        for (index = 0;index <= cnt;index++) {
                count = fwrite(message,sizeof(message),1,fp);
                fflush(fp);
                if (count != 1) {
                        printf("Error writing to file example\n");
                } else {
                        printf("data written to file example successfully\n");
                }
                fstat(fileno(fp), &sbuf);
                printf("Links to example: %d\n", sbuf.st_nlink);

                sleep(20);
        }
        fclose(fp);
        return(00);
}

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-11-2013
rm uses the unlink system call to "delete" a file. The unlink(2) manual page usually documents the behavior which Don described.

If you also wanted to watch for replacement, you could record the original inode with fstat then monitor the pathname with stat: failure && errno == ENOENT -> file unlinked, success && original_inode != current_inode -> file replaced.

For more efficient but more complicated and unportable monitoring, refer to Linux's inotify or BSD's kqueue.

Regards,
Alister

Last edited by alister; 07-11-2013 at 01:47 PM..
This User Gave Thanks to alister For This Post:
# 4  
Old 07-12-2013
Linux The fwrite function is not returning error, if the file was removed.

Thanks Don and Alister, for your help !!!
Thanks againSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell equivalent of matlab fwrite function

I have some matlab code that sends the serial port elements of an array using matlab's fwrite function, e.g.: fwrite(s, , 'uchar'); What would be a unix shell equivalent? E.g., after successfully accessing the port using instructions here: Simple terminal serial port program for Linux/MacOSX... (3 Replies)
Discussion started by: darwin_886
3 Replies

2. Shell Programming and Scripting

Error removed from file

Below is a flowchart of a program. Most everything works as expected, but there are a couple of issues that I need some expert help on. The check function was setup initially for a single user input. The input has been modified to allow for multiple inputs, so the code below does not work. My... (15 Replies)
Discussion started by: cmccabe
15 Replies

3. Programming

Function Returning Pointer

Hi guys. how a functions such fdopen, ... can return pointer? are these functions use static memory(variables)? (6 Replies)
Discussion started by: majid.merkava
6 Replies

4. Programming

segmentation fault while returning from function.

I am working on the application in which I have to fetch values from the database and paste in url and send it to portal. table=get_result("SELECT serialno,cas,Mode,FLC,TLC,location,CompName,CompCode,FG,FC,DispNo,TruckNo,LWbill,RRGPN,INVNO,DCN,RQTY,DQTY,SQTY,DDATE,RDATE,SDATE,TTIME FROM... (1 Reply)
Discussion started by: er.rohan88
1 Replies

5. Programming

segmentation fault in fwrite function

Hi, my code is written in proC and it is in UNIX(AIX).I have written a small code for writing data into a binary file,but while writing my program is giving core dump. Here Is my code---- fpWriteFile = fopen(WriteFileName,"wb+"); CHAR *recvgen; recvgen = (char... (7 Replies)
Discussion started by: ajaysahoo
7 Replies

6. Shell Programming and Scripting

Returning the name of function used

Hi All In my script, I can call on several functions. I have a logging function that is called by any of these functions. What I would like is some way of identifying which function I am using and pass this to the log function as some parameter. Is there some built in command or way of... (3 Replies)
Discussion started by: kingpin2502
3 Replies

7. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

8. Shell Programming and Scripting

returning from a function

Hi all, I am very new to BASH shell programming. I need to return an integer from a function to the caller function. I did this: but it keeps giving me wrong return: Can someone help me out here, please? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

9. Programming

create a thread from a returning function

hi all, my requirement is to create a thread by calling another function. i.e i dont call pthread_create directly from main, but by calling another function (createThd - below ), from main. Example: void *thread_function(void *arg) { /* thread function */ int i; rc =... (3 Replies)
Discussion started by: wolwy_pete
3 Replies

10. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies
Login or Register to Ask a Question