检查文件是否在 C 中打开
Check if the file was opened in C
我正在尝试编写一个函数来检查文件是否已打开,如果没有,则打开它。该文件在 main 中定义,并作为参数传递给函数。代码有效,但是当我尝试向主函数添加一些东西时(就像 int i; 一样简单),程序崩溃并且错误消息是:Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 在带有“if (*subor==NULL) {”的行中因此我认为文件的存储方式存在问题。
这是我的代码:
int funkcia_v (FILE **subor) {
if (*subor==NULL) {
printf("File has not been opened yet.\n");
*subor = fopen("pathtothefile.txt","r");
if (*subor==NULL) {
printf("File not opened.\n");
}
}
else {
printf("File already opened.\n");
}
printf("\n");
return 0;
}
int main() {
char c;
//if i type int i; here, the program crashes
FILE* subor;
printf("Start the function.\n");
c = getchar();
while (1) {
if (c=='v') {
funkcia_v(subor);
}
else if (c=='k') {
break;
}
else {
printf("Unknown key, try again.");
printf("\n");
}
fflush(stdin);
c = getchar();
}
return 0;
}
有什么想法吗?
因此,结合以上所有建设性意见,
我们有这个。
#include <stdio.h>
#include <stdlib.h>
int funkcia_v (FILE **subor) {
if (*subor==NULL) {
printf("File has not been opened yet.\n");
*subor = fopen("pathtothefile.txt","r");
if (*subor==NULL) {
printf("File not opened.\n");
}
else printf("File just opened.\n");
}
else {
printf("File already opened.\n");
}
printf("\n");
return 0;
}
int main() {
char c;
int i; // doesn't crash
FILE* subor = NULL;
printf("Start the function.\n");
c = getchar();
while (1) {
if (c=='v') {
funkcia_v(&subor);
}
else if (c=='k') {
break;
}
else if (c!=10) {
printf("Unknown key %c, try again.",c);
printf("\n");
}
c = getchar();
}
if (subor!=NULL) fclose (subor);
return 0;
}
请注意,它将文件指针的指针传递给函数。
我正在尝试编写一个函数来检查文件是否已打开,如果没有,则打开它。该文件在 main 中定义,并作为参数传递给函数。代码有效,但是当我尝试向主函数添加一些东西时(就像 int i; 一样简单),程序崩溃并且错误消息是:Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 在带有“if (*subor==NULL) {”的行中因此我认为文件的存储方式存在问题。
这是我的代码:
int funkcia_v (FILE **subor) {
if (*subor==NULL) {
printf("File has not been opened yet.\n");
*subor = fopen("pathtothefile.txt","r");
if (*subor==NULL) {
printf("File not opened.\n");
}
}
else {
printf("File already opened.\n");
}
printf("\n");
return 0;
}
int main() {
char c;
//if i type int i; here, the program crashes
FILE* subor;
printf("Start the function.\n");
c = getchar();
while (1) {
if (c=='v') {
funkcia_v(subor);
}
else if (c=='k') {
break;
}
else {
printf("Unknown key, try again.");
printf("\n");
}
fflush(stdin);
c = getchar();
}
return 0;
}
有什么想法吗?
因此,结合以上所有建设性意见, 我们有这个。
#include <stdio.h>
#include <stdlib.h>
int funkcia_v (FILE **subor) {
if (*subor==NULL) {
printf("File has not been opened yet.\n");
*subor = fopen("pathtothefile.txt","r");
if (*subor==NULL) {
printf("File not opened.\n");
}
else printf("File just opened.\n");
}
else {
printf("File already opened.\n");
}
printf("\n");
return 0;
}
int main() {
char c;
int i; // doesn't crash
FILE* subor = NULL;
printf("Start the function.\n");
c = getchar();
while (1) {
if (c=='v') {
funkcia_v(&subor);
}
else if (c=='k') {
break;
}
else if (c!=10) {
printf("Unknown key %c, try again.",c);
printf("\n");
}
c = getchar();
}
if (subor!=NULL) fclose (subor);
return 0;
}
请注意,它将文件指针的指针传递给函数。