02-12-07 12:20 PM
Hi,
Below is a simple code:
#include <errno.h>
#include <fcntl.h>
#include <iostream.h>
using namespace std;
int main()
{
int handle;
char *BUF = "HELLO, HELLO[20]";
char b[10];
handle = creat("t/a", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);//
O_RDWR );
int ret = write(handle, BUF,
strlen(BUF)); //
LINE1
cout << "ret:" << ret << endl;
cout << "BUF:" << BUF << endl;
int err;
int r = read(handle,b,
10);
//
LINE2
if ( r != 10 )
{
cout<<"r:"<<r<<" Errno:" <<errno<<" b:"<<b<<endl;
}
return 0;
}
The output of the code is:
> ./test
ret:16
BUF:HELLO, HELLO[20]
r:-1 Errno:9 b:T-??T-??T-??T-??T-??T-??T-??T-??
Why the read( ) at LINE2 failed, but the write( ) at LINE1 succeeded.
The errno 9 is:
#define EBADF 9 /* Bad file descriptor */
The file handle works for write( ), but does not work for read( ).
Why?
The system is IBM AIX. The compiler is xlC.
Thanks.
Jack
[ Post a follow-up to this message ]
|