int open(const char *path, int oflags, mode_t mode);
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
int file_desc;
int save_errno;
file_desc = open("/tmp/LCK.test", O_RDWR | O_CREAT | O_EXCL, 0444);
if(file_desc == -1) {
save_errno = errno;
printf("Open failed with error %d\\n", save_errno);
}
else {
printf("Open succeeded\\n");
}
exit(EXIT_SUCCESS);
}
beyes@linux-beyes:~/C> ./lock1.exe
Open succeeded
beyes@linux-beyes:~/C> ./lock1.exe
Open failed with error 17
beyes@linux-beyes:~/C> cat lock1.c
#include <stdio.h>
int main()
{
int input;
printf("input a integer: ");
scanf("%d", &input);
printf("%d\\n", input);
return 0;
}
beyes@linux-beyes:~/C> ./open2.exe
input a integer: 88
88
#include <stdio.h>
int main()
{
int input;
close(0); /* 关闭标准输入 */
printf("input a integer: ");
scanf("%d", &input);
printf("%d\\n", input);
return 0;
}
beyes@linux-beyes:~/C> ./open2.exe
input a integer: -1208172000
#include <stdio.h>
int main()
{
int input;
close(1); /* 关闭标准输入 */
printf("input a integer: ");
scanf("%d", &input);
printf("%d\\n", input);
return 0;
}
#include <stdio.h>
#include <fcntl.h>
int main()
{
int file_desc;
close(0);
file_desc = open("test.txt", O_RDWR | O_CREAT, 0666);
printf("The file descriptor is: %d\\n", file_desc);
return 0;
}
beyes@linux-beyes:~/C> ./open2.exe
The file descriptor is: 0
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |