include <fcntl.h>
include <unistd.h>
int main()
{ int file_desc = open("test.txt", O_RDONLY);
if (file_desc < 0)
{ // 错误处理 }
// 进行其他操作... close(file_desc); return 0;}
include <fcntl.h>
include <unistd.h>
int main()
{ char buffer[256];
int file_desc = open("test.txt", O_RDONLY);
if (file_desc < 0)
{ // 错误处理 }
ssize_t bytes_read = read(file_desc, buffer, sizeof(buffer) - 1);
if (bytes_read < 0)
{ // 错误处理 } buffer[bytes_read] = '\0';
// 添加null字符以确保是一个有效的C字符串 // 进行其他操作... close(file_desc);
return 0;
}
include <fcntl.h>
include <unistd.h>
int main()
{ char* message = "Hello, Linux!\n";
int file_desc = open("test.txt", O_WRONLY | O_CREAT, S_IRWXU);
if (file_desc < 0)
{ // 错误处理 } ssize_t bytes_written = write(file_desc, message, strlen(message));
if (bytes_written < 0)
{ // 错误处理 } // 进行其他操作... close(file_desc);
return 0;
}