Home>
int main () {
int data1, data2;
data1 = open ("data1.txt", O_WRONLY);
data2 = open ("data2.txt", O_RDONLY);
printf ("% d \ n", data1);
printf ("% d \ n", data2);
close (data1);
close (data2);
}


Assuming that the files data1.txt and data2.txt exist, when the above program is executed, "3" and "4" are output. What does this mean? Even if the contents of data1.txt and data2.txt were rewritten, the output result did not change, so I knew that the contents of the file were irrelevant ...

c
  • Answer # 1

    Isn't it a file descriptor?
    Read about here and google it.
    Understanding file descriptors
    Man page of OPEN

  • Answer # 2

    The file descriptor is already in the answer.

    Although it is often returned "What file/device is open?"
    It's not guaranteed by the specification, so it doesn't mean much more than just a number.

    I think that changing the order of opening will change the numbers.

    By the way, I think that standard input is 0, standard output is 1, standard error output is 2.


    Note thatopen/closehas compatibility issues, so if it is a file input/output,fopen/fcloseis recommended.