c - 检查之前是否调用过函数
问题描述
这是我正在尝试使用的基本代码。
void test(){
FILE *input;
input = fopen("input.txt.", "r");
}
所以我试图检查文件之前是否已经打开过,这意味着之前调用过一次 void test() 函数。我真的不知道该怎么做,我尝试了while和if。像这样。
void test(){
FILE *input;
int open = 0;
while (open == 0){
input = fopen("input.txt", "r");
if (input == NULL){
printf("File wasnt opened.\n");
}
if (input != NULL){
printf("File is opened.\n");
}
open = open + 1;
}
if(open!=0){
printf("file is already opened.\n");
}
}
解决方案
使用局部静态变量。
void test (void)
{
static bool called_before = false;
if(called_before)
{
do_this();
}
else
{
do_that();
called_before = true;
}
}
推荐阅读
- javascript - Stripe - 如何使用 Checkout Session (php/js) 获取订阅 ID(以取消)
- laravel - 在 Laravel 6 中调用未定义的方法 stdClass::isOnline()
- laravel - 如何在自定义规则中验证电子邮件
- android - 单击后禁用Android cordova InAppBrowser提交按钮
- reactjs - 首次在 React.js 中渲染时如何避免 init 函数调用?
- oracle - 使用 SQL Developer 连接 do DB 时没有匹配的身份验证协议
- reactjs - 提升输入字段的状态后,无法在字段中输入任何内容
- javascript - 我可以在 html 文件中使用 nodejs 库吗?
- java - 根据多个条件替换属性值
- angular - Angular子调用父函数然后父将数据传递给子