在C语言中,是否仍然可以在if条件之外使用文件指针
我的问题从while循环开始。我有一个if条件,在在C语言中,是否仍然可以在if条件之外使用文件指针,c,if-statement,pointers,global-variables,cs50,C,If Statement,Pointers,Global Variables,Cs50,我的问题从while循环开始。我有一个if条件,在if条件的内部,我创建一个文件并写入它。但是很自然,我不能在条件之外使用指针。我是C语言的新手,我正在寻找一种使指针成为python中的全局变量的方法。这是我的密码: #include <stdio.h> #include <stdlib.h> #include "cs50.h" //define chunk size const int chunksize = 512; int main(int
if
条件的内部,我创建一个文件并写入它。但是很自然,我不能在条件之外使用指针。我是C语言的新手,我正在寻找一种使指针成为python中的全局变量的方法。这是我的密码:
#include <stdio.h>
#include <stdlib.h>
#include "cs50.h"
//define chunk size
const int chunksize = 512;
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr,"Usage: ./recover image\n");
return 1;
}
// open memory card file
FILE *inptr = fopen(argv[1], "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
//create a variable to store memory size
fseek(inptr, 0L, SEEK_END);
long int memorysize = ftell(inptr);
rewind(inptr);
//find how many chunk does memory card contain
int nofchunks = memorysize / chunksize;
//create a file counter
int nofjpeg = 0;
while(nofchunks > 0)
{
//create a temporary storage
unsigned char chunk[chunksize];
// read a 512 byte chunk from memory card
fread(&chunk, chunksize, 1, inptr);
FILE *outptr = NULL;
//check the chunk if it is a JPEG by looking first 4byte of the chunk
if (chunk[0] == 0xff && chunk[1] == 0xd8 && chunk[2] == 0xff && (chunk[3] & 0xf0) == 0xe0)
{
nofjpeg++;
//create a temporary file name
char filename[8];
if (nofjpeg == 1)
{
//create a new file name
sprintf(filename, "%03i.jpg", nofjpeg);
//open the file in write mode
outptr = fopen(filename, "w");
if (outptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
fwrite(&chunk, chunksize, 1, outptr);
}
else
{
//close the previous file
fclose(outptr);
//create a new file name
sprintf(filename, "%03i.jpg", nofjpeg);
//open the file in write mode
outptr = fopen(filename, "w");
if (outptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
fwrite(&chunk, chunksize, 1, outptr);
}
}
else if(nofjpeg > 1)
{
fwrite(&chunk, chunksize, 1, outptr);
}
nofchunks--;
}
}
#包括
#包括
#包括“cs50.h”
//定义块大小
常量int chunksize=512;
int main(int argc,char*argv[])
{
如果(argc!=2)
{
fprintf(stderr,“用法:./recover image\n”);
返回1;
}
//打开存储卡文件
文件*inptr=fopen(argv[1],“r”);
如果(inptr==NULL)
{
fprintf(stderr,“无法打开%s。\n”,argv[1]);
返回2;
}
//创建一个变量来存储内存大小
fseek(inptr,0L,SEEK_END);
long int memorysize=ftell(inptr);
倒带(inptr);
//找出内存卡包含多少块
int nofchunks=memorysize/chunksize;
//创建一个文件计数器
int nofjpeg=0;
而(nofchunks>0)
{
//创建临时存储
无符号字符块[chunksize];
//从存储卡读取512字节的数据块
fread(&chunk,chunksize,1,inptr);
文件*outptr=NULL;
//通过查看区块的前4个字节检查区块是否为JPEG
如果(块[0]==0xff&&chunk[1]==0xd8&&chunk[2]==0xff&&chunk[3]&0xf0)==0xe0)
{
nofjpeg++;
//创建一个临时文件名
字符文件名[8];
如果(nofjpeg==1)
{
//创建一个新的文件名
sprintf(文件名,“%03i.jpg”,nofjpeg);
//以写模式打开文件
outptr=fopen(文件名,“w”);
if(outptr==NULL)
{
fprintf(stderr,“无法打开%s。\n”,argv[1]);
返回2;
}
fwrite(&chunk,chunksize,1,outptr);
}
其他的
{
//关闭上一个文件
fclose(outptr);
//创建一个新的文件名
sprintf(文件名,“%03i.jpg”,nofjpeg);
//以写模式打开文件
outptr=fopen(文件名,“w”);
if(outptr==NULL)
{
fprintf(stderr,“无法打开%s。\n”,argv[1]);
返回2;
}
fwrite(&chunk,chunksize,1,outptr);
}
}
否则如果(nofjpeg>1)
{
fwrite(&chunk,chunksize,1,outptr);
}
野猪--;
}
}
您可以看到,在第一个内部
条件的内部,我打开文件并对其进行写入。我需要在下面的else
条件中关闭该文件。您还可以看到,我也在外部if条件之后使用该指针。在if
语句之前声明文件指针,但为其指定一个null
值。然后在if
语句之后可用。在if
语句中,您可以为其分配一个值,该值将在if
语句之后持续存在。在if
语句之后,在取消引用它之前,您应该检查以确保它不是null
//check the chunk if it is a JPEG by looking first 4byte of the chunk
FILE *outptr = (FILE*) NULL; // Declare before the if and assign to NULL cast to type FILE*
if (chunk[0] == 0xff && chunk[1] == 0xd8 && chunk[2] == 0xff && (chunk[3] & 0xf0) == 0xe0)
{
nofjpeg++;
//create a temporary file name
char filename[8];
if (nofjpeg == 1)
{
//create a new file name
sprintf(filename, "%03i.jpg", nofjpeg);
//open the file in write mode
outptr = fopen(filename, "w"); // Assign inside the if statement, but don't declare.
if (outptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
fwrite(&chunk, chunksize, 1, outptr);
}
else
{
//close the previous file ... this is likely a logical error; it should be NULL at this point, so there's nothing to close.
if (outptr) fclose(outptr); // Check to see if pointer is null before use.
//create a new file name
sprintf(filename, "%03i.jpg", nofjpeg);
//open the file in write mode
outptr = fopen(filename, "w");
if (outptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
fwrite(&chunk, chunksize, 1, outptr);
}
}
else if(nofjpeg > 1)
{
// This is likely a logical error; outptr will be NULL here
if (outptr) fwrite(&chunk, chunksize, 1, outptr);
}
nofchunks--;
我试过了,但我得到了这个错误:用一个不兼容类型'void*'FILE outptr=NULL的表达式初始化'FILE'(也称为'struct\u IO\u FILE');编辑。。。您应该能够将其强制转换为FILE*指针。在C中,完全不需要强制转换NULL
(在此上下文中)。OP可能是用C++编译器编译C代码。@是的,我忘了星星。现在我更正了它,但我得到了一个新错误:声明一个局部变量[-Werror,-Wshadow]FILE*outptr=fopen(文件名,“w”);recover.c:43:15:注意:前面的声明在这里FILE*outptr=NULL@BaranAldemir对,阴影表示在内部作用域中声明一个变量,而在外部作用域中隐藏另一个同名变量。为了避免这种情况,您需要删除声明FILE*outptr=fopen(文件名,“w”)代码>因此它变成outptr=fopen(文件名,“w”)在if语句的两个分支中都使用code>。除此之外,您可能还有一些逻辑错误,我没有解决这些错误,因为您可能有一些理由按照您的方式构建事物。