-
write函數
鎖定
- 中文名
- write函數
- 類 型
- C語言函數
- 函數名
- write
- 功 能
- 將數據寫入已打開的文件內
write函數用 法
write有兩種用法。一種是:ssize_t write(int fd, const void *buf, size_t nbyte);
nbyte:要寫入文件指定的字節數;
返回值:寫入文檔的字節數(成功);-1(出錯)
write函數把buf中nbyte寫入文件描述符handle所指的文檔,成功時返回寫的字節數,錯誤時返回-1.
另一種是: write(const char* str,int n)
str是字符指針或字符數組,用來存放一個字符串。n是int型數,它用來表示輸出顯示字符串中字符的個數。
write函數程序例
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys\stat.h> #include <io.h> #include <string.h> int main(void) { int handle; char string[40]; int length, res; /* Create a file named "TEST.$$$" in the current directory and write a string to it. If "TEST.$$$" already exists, it will be overwritten. */ if ((handle = open("TEST.$$$", O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE)) == -1) { printf("Error opening file.\n"); exit(1); } strcpy(string, "Hello, world!\n"); length = strlen(string); if ((res = write(handle, string, length)) != length) { printf("Error writing to the file.\n"); exit(1); } printf("Wrote %d bytes to the file.\n", res); close(handle); return 0; }
- 詞條統計
-
- 瀏覽次數:次
- 編輯次數:35次歷史版本
- 最近更新: suddean