Strange read problem
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Strange read problem




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Strange read problem  
Jack


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Re: Strange read problem  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-12-07 12:20 PM

2007-02-12, 00:15(-08), Jack:
[...]
>    handle = creat("t/a", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);//
> O_RDWR  );
[...]
> 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?
[...]

creat() is to open a file in write-only mode. It's equivalent to
open(file, O_CREAT|O_WRONLY|O_TRUNC, mode). So you can't read
from a file open that way.

Use open(2) if you want to open a file in read-write mode. And
beware that when you do so, there's only one /cursor/ position
within the file. You can't read what you just wrote unless you
rewind within the file.

--
Stéphane





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:11 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register