我可以用不同的代码声明并打开一个文件,就像在此问题打开后需要继续写作
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int blocksize = 512;
int main(int argc, char *argv[])
{
if( argc != 2 )
{
printf("mention the file name or too many commands\n");
return 1;
}
FILE *in = fopen(argv[1], "r");
FILE *out;
if (in == NULL)
{
printf("could not open the file %s", argv[1]);
return 1;
}
uint8_t buffer[blocksize];
int count = 0;
int precount = 1;
int flag = 1;
while(fread(buffer, sizeof(uint8_t), 512, in) == blocksize)
{
if((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3] & 0xf0) == 0xe0)) || flag == 1)
{
if (flag == 0)
{
char filename[9];
sprintf(filename, "%03i.jpg", count);
out = fopen(filename, "w");
count++;
flag = 1;
}
if(count == precount)
{
fwrite(buffer, sizeof(uint8_t), 512, out);
}
else
{
fclose(out);
precount++;
flag = 0;
}
}
}
return 0;
}
首先,我要搜索标题,然后找到标题,然后将其写入out
文件中。
我想继续写作,直到再次找到所需的标题,然后我想关闭现有文件并打开一个新文件,直到再次找到这些标题为止。
现在问题是当我在if语句中声明file *out
时,它将变成if if if inf local变量,如果我按照此操作的方式执行此操作,我会得到SEG错误。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int blocksize = 512;
int main(int argc, char *argv[])
{
if( argc != 2 )
{
printf("mention the file name or too many commands\n");
return 1;
}
FILE *in = fopen(argv[1], "r");
FILE *out;
if (in == NULL)
{
printf("could not open the file %s", argv[1]);
return 1;
}
uint8_t buffer[blocksize];
int count = 0;
int precount = 1;
int flag = 1;
while(fread(buffer, sizeof(uint8_t), 512, in) == blocksize)
{
if((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3] & 0xf0) == 0xe0)) || flag == 1)
{
if (flag == 0)
{
char filename[9];
sprintf(filename, "%03i.jpg", count);
out = fopen(filename, "w");
count++;
flag = 1;
}
if(count == precount)
{
fwrite(buffer, sizeof(uint8_t), 512, out);
}
else
{
fclose(out);
precount++;
flag = 0;
}
}
}
return 0;
}
First I am searching for headers then once I find it, I write it in the out
file.
I want to keep writing until I find the required headers again and then I want to close the existing file and open a new file until I find those headers again.
Now the problem is when I declare the FILE *out
inside the if statement it becomes if's local variable and if I do this the way the code here I get seg fault.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论