CBFalconer said:
smarty said:
this code gives an "illegal seek error" on close() call
what is this error and when does it come?
main()
{
int fd,num;
char buf[150];
fd = open ("123.c",O_RDWR);
if(fd!=-1)
{
printf("a file with fd=%d is opened\n",fd);
num=read(fd,buf,150);
printf("num=%d\nREAD:%s",num,buf);
perror("READ");
close(fd);
perror("CLOSE");
}
}
Who knows. There are no such functions as 'open', 'read', 'close'
in standard C. Look up, and use, fopen and fclose, and check the
various standard library calls for means of reading numbers.
Those are POSIX functions. The OP might well have a good reason to
use them instead of the C standard functions fopen, fread, and fclose
(the POSIX functions provide some features that the standard C
functions don't). The quoted program doesn't demonstrate any such
reason, but it's obviously just a sample.
But yes, it's generally better to use the standard C I/O functions
*unless* you have a specific reason to use the POSIX functions and pay
the price of losing some portability.
(Incidentally, the program has nothing to do with reading numbers; num
is the number of bytes read.)
Some incidental advice for the original poster:
Write "int main(void)" rather than "main()".
Adding a blank after each comma, after the "if" keyword, and before
and after each binary operator, aids readability.
Since main returns an int, it should do so: add "return 0;" before the
closing brace. There are circumstances in which this is not necessary
(if you're using a C99 implementation, or if you don't care about the
status returned to the host environment), but it never hurts, and it's
a good habit.
And, of course, you should examine the value of errno only after you
know a function has failed (but check the function's documentation to
find out whether it even sets errno).