dont understand dup - Minix
This is a discussion on dont understand dup - Minix ; hi
im trying to play with dup and see how it works out and i've got one
question:
why does the following NOT work?
#include
#include
void main(void)
{
int i;
i = dup(1);
fprintf(i, "Hi, I'm the new descriptor ...
-
dont understand dup
hi
im trying to play with dup and see how it works out and i've got one
question:
why does the following NOT work?
#include
#include
void main(void)
{
int i;
i = dup(1);
fprintf(i, "Hi, I'm the new descriptor and my value is %i\n", i);
//supposed to be displayed on terminal
}
Regards,
Martin
-
Re: dont understand dup
> im trying to play with dup and see how it works out and i've got one
> question:
> why does the following NOT work?
>
> #include
> #include
>
> void main(void)
> {
> int i;
> i = dup(1);
> fprintf(i, "Hi, I'm the new descriptor and my value is %i\n", i);
> //supposed to be displayed on terminal
> }
Your code gives a warning when compiled: "illegal conversion of int to
pointer". This should give you a clue.
The problem is that fprintf expects a stream (of type FILE *) rather
than a file descriptor (of type int).
Replace the fprintf to get a result:
write(i, "Hello world\n", 12);
-
Re: dont understand dup
Whats the difference between a file descriptor and a file pointer?
Whats the point (pardon my pun) in having two such things?
Avatar Zondertau wrote:
> > im trying to play with dup and see how it works out and i've got one
> > question:
> > why does the following NOT work?
> >
> > #include
> > #include
> >
> > void main(void)
> > {
> > int i;
> > i = dup(1);
> > fprintf(i, "Hi, I'm the new descriptor and my value is %i\n", i);
> > //supposed to be displayed on terminal
> > }
>
> Your code gives a warning when compiled: "illegal conversion of int to
> pointer". This should give you a clue.
>
> The problem is that fprintf expects a stream (of type FILE *) rather
> than a file descriptor (of type int).
>
> Replace the fprintf to get a result:
>
> write(i, "Hello world\n", 12);
-
Re: dont understand dup
sancho1980 wrote:
> Whats the difference between a file descriptor and a file pointer?
> Whats the point (pardon my pun) in having two such things?
write() is part of POSIX, fprintf is part of C std lib.
-
Re: dont understand dup
sancho1980 wrote:
> Whats the difference between a file descriptor and a file pointer?
> Whats the point (pardon my pun) in having two such things?
> Avatar Zondertau wrote:
>>> im trying to play with dup and see how it works out and i've got one
>>> question:
>>> why does the following NOT work?
>>>
>>> #include
>>> #include
>>>
>>> void main(void)
>>> {
>>> int i;
>>> i = dup(1);
>>> fprintf(i, "Hi, I'm the new descriptor and my value is %i\n", i);
>>> //supposed to be displayed on terminal
>>> }
>> Your code gives a warning when compiled: "illegal conversion of int to
>> pointer". This should give you a clue.
>>
>> The problem is that fprintf expects a stream (of type FILE *) rather
>> than a file descriptor (of type int).
>>
>> Replace the fprintf to get a result:
>>
>> write(i, "Hello world\n", 12);
>
FILE * is a struct used internally by the libc to maintain file
descriptors. It also holds other information, such as buffer status and
other such, making it easy to write cross-platform APIs.
A file descriptor is basically a number that points to a file descriptor
structure that exists outside of your program (either the kernel or
filesystem server, depending on the OS). You can create a FILE * from a
file descriptor by using fdopen(3). You can then use fwrite(3) and
fread(3) to read the descriptor. Alternatively, you can usr write(2) and
read(2) directly, but that is not always the total solution.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFOZsjsjeOFtd+nycRApT6AJwJ6xaWbOC+2nokI/j+Zd5QRiyR2QCfQdaO
r8JWrAG4TR5tcWNYjYQdwzc=
=Biu5
-----END PGP SIGNATURE-----