gpt4 book ai didi

c - 如何创建一个文本文件,在给定路径中该文件不存在

转载 作者:行者123 更新时间:2023-11-30 17:01:26 24 4
gpt4 key购买 nike

void my_create(char* path)
{
FILE* fp;
fp = fopen(path, "rb+");
if (fp == NULL) /* File doesn't exist*/
fp = fopen(path, "wb");
}

为什么不起作用?或者我由于给定的错误路径而做错了什么?不太确定。

提前致谢

最佳答案

文件名可能 Not Acceptable ,或者您遇到权限问题。

试试这个:

#include <string.h>
#include <errno.h>

void my_create(char* path)
{
FILE* fp;
fp = fopen(path, "rb+");
if (fp == NULL) { /* File doesn't exist*/
printf ("File does not exist : %s", path)
fp = fopen(path, "wb+");
if (fp == NULL) {
fprintf (stderr, "Cannot create file : %s\n", path);
fprintf (stderr, "Reason : %s" , strerror (errno));
}
}
}

关于c - 如何创建一个文本文件,在给定路径中该文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37074198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com