0

Consider i have opened the file as hell.txt with the open() function.

  fd=open("hell.txt",O_RDONLY);

then, consider it will return the descriptor as 4. and hi.txt already occupy the descriptor 3, so i want to connect the hell.txt file with 3 without using dup2 or fcntl() function.

is it possible to change without using that two functions?

Bhuvanesh
  • 1,269
  • 1
  • 15
  • 25
  • What is the context? What do you want to do with file descriptor `3` afterwards? The simplest way to connect hell.txt to `3` is to close hi.txt and open hell.txt for the second time (assuming standard file descriptors 0-2 are open) – jfs Jan 12 '15 at 11:30
  • is dup3() allowed :^) – Jasen Jan 12 '15 at 11:44

1 Answers1

0

because file descriptrs are usually occupied lowest number first this usually works:

close(3);
fd=open("hell.txt",O_RDONLY); 
Jasen
  • 11,837
  • 2
  • 30
  • 48
  • *"without using dup2 or fcntl() function."* -- I assume it also includes `dup()` – jfs Jan 12 '15 at 11:50