演示版本
VS2012
- close()函數
close()函數用於關閉由open()函數所打開的文件。
語法
int close(int handle);
close()函數的語法參數說明如下:
參數handle為打開文件時所返回的文件句柄。
close()函數成功關閉文件返回0,否則返回-1。
示例
#include <stdio.h> #include <string> #include <io.h> int main() { char filename[80]; char buf[100]=""; int file; printf("input file path and file name,eg d:\\1\\1\\a.txt ");//顯示提示 gets(filename);//輸入文件名 file=_creat(filename,0);//創建文件 if (file==-1)//-1表示出錯 { printf("create file error"); exit(0); } printf("input file content:,end with \"##\":\n");//輸入文件內容,##表示結束 while (1) { gets(buf);//輸入一行 strcat(buf, "\r\n");//加入換行符 if (strcmp(buf, "##\r\n")==0)//遇到##退出 break; _write(file, buf, strlen(buf)); } _close(file);//關閉文件 }
阿飛
2021年8月3日