首页 > 编程语言 > 详细

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

时间:2021-08-03 22:30:22      阅读:26      评论:0      收藏:0      [点我收藏+]

演示版本

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日

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!