我可以用不同的代码声明并打开一个文件,就像在此问题打开后需要继续写作

发布于 2025-01-25 21:33:39 字数 1462 浏览 3 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文