Skip navigation.
Home
The QNX Community Portal

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

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

anything that doesn't fit to other groups.

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

Postby ger » Fri Apr 10, 2015 5:26 am

Hi,

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;

COUNT counts[5];

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

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

Geoff.
ger
Active Member
 
Posts: 13
Joined: Tue Jul 15, 2014 9:24 am

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

Postby Tim » Fri Apr 10, 2015 4:35 pm

Geoff,

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.

Tim
Tim
Senior Member
 
Posts: 1390
Joined: Wed Mar 10, 2004 12:28 am

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

Postby denkelly » Sun Apr 12, 2015 12:24 am

According to the docs, errno should not be checked unless -1 is returned (which was not in your case).
denkelly
Senior Member
 
Posts: 161
Joined: Sat Aug 02, 2008 3:27 pm

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

Postby ger » Wed Apr 15, 2015 4:31 am

Yes, well after all these years I still learn things! :-) (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!

Thanks.

Geoff.
ger
Active Member
 
Posts: 13
Joined: Tue Jul 15, 2014 9:24 am


Return to General Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron