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 \"##\":\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日


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM