我希望我的程序能够打开一个已经存在的文件,如果不存在则创建该文件。我正在尝试以下代码,但是在 freopen.c 中出现了调试断言(debug assertion)。我是否最好使用 fclose 然后立即使用 fopen?
FILE *fptr;
fptr = fopen("scores.dat", "rb+");
if(fptr == NULL) //if file does not exist, create it
{
freopen("scores.dat", "wb", fptr);
}