errno value non-zero following successful [f]write()


Maybe in my old age (with possible onset of dementia!) I have forgotton some fundamentals of C and C++ behaviour.

Consider the following [very loose] code please:

typedef struct
char v[1500];

COUNT counts[5];

int fd = open(“somefile”, O_WRONLY);
errno = EOK;
int l = write(fd, &counts, sizeof(counts));
printf(“errno = %d\n”, errno);

Assume the file opens OK (fd != -1) and that write() returns 7500 (5 x 1500) and not -1.

However, after the apparently successful write, errno is 3 “No such process”. Same occurs if I use fa file descriptor (fopen, fwrite, etc).

How is this? Is errno undefined on a successful file operation?

BTW, using QNX 6.5.0 SP1



errno is a global variable for your process (or thread if you are threaded).

Even if YOUR code (the open and write calls) didn’t produce any error (and thus generate a value in errno) you have no idea whether some library call in the open/write routines did in fact encounter an error (or other condition) and thus fill out errno based on THEIR return calls (it may be that your file didn’t exist and that was detected and returned in errno to some library call which then generated calls to create the file vs overwrite. Or perhaps is a status return based on checking if you have permission to create a file in that directory etc).

Basically errno is undefined if the calls you make (open/write) return successfully.


According to the docs, errno should not be checked unless -1 is returned (which was not in your case).

Yes, well after all these years I still learn things! :slight_smile: (or maybe re-remember them)

Noting that errno is global, and set to 0 immediately prior to the write() call, it seemed strange to me that regardless of what else may be happening (such as in another thread) and what the “docs” say, the value is consistently changed to 3 even though nothing actually went wrong.

I accept that it’s best that errno not be checked unless a -1 is returned, but if there is no error anywhere why would it be changed away from 0 at all?

Whatever, that clears that one up!