C语言创建文件

问题需求:使用程序创建一个文件(当该文件不存在时则创建)。

代码如下:

// 创建文件
void CreateFile()
{
    //文件指针
    FILE *fileP;
    char fileName[] = "hello.txt";      //保存在工程目录下

    //使用“读入”方式打开文件
    fileP = fopen(fileName, "r");

    //如果文件不存在
    if (fileP == NULL)
    {
        //使用“写入”方式创建文件
        fileP = fopen(fileName, "w");
    }
    
    //关闭文件
    fclose(fileP);
}

void main()
{
    CreateFile();

    system("pause");
}
posted @ 2019-03-31 11:36  小小一步  阅读(21679)  评论(0编辑  收藏  举报