C语言常用函数-close()关闭文件函数

演示版本

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 "##":
");//输入文件内容,##表示结束
    while (1)
    {
        gets(buf);//输入一行    
        strcat(buf, "
");//加入换行符    
        if (strcmp(buf, "##
")==0)//遇到##退出
            break;

        _write(file, buf, strlen(buf));
    }
    _close(file);//关闭文件

}

阿飞

2021年8月3日

原文地址:https://www.cnblogs.com/nxopen2018/p/15096306.html