Stuck with recover (cs50 pset4)

huangapple go评论48阅读模式
英文:

Stuck with recover (cs50 pset4)

问题

这是我的当前代码。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t BYTE;
int const BLOCK_SIZE = 512;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("用法不正确\n");
        return 1;
    }

    FILE *file = fopen(argv[1], "r");
    if (file == NULL)
    {
        printf("无法打开文件\n");
        return 1;
    }

    BYTE buffer[512];
    int indx = 0;
    int irratation = 0;
    char *filename = malloc(10);

    while (fread(buffer, 1, BLOCK_SIZE, file) == BLOCK_SIZE)
    {
        FILE *ptr = NULL;

        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            irratation++;
            if (irratation >= 2)
            {
                indx++;
            }

            if (indx == 0)
            {
                sprintf(filename, "%03i.jpg", indx);
                ptr = fopen(filename, "w");
                fwrite(buffer, 1, BLOCK_SIZE, ptr);
            }
            else
            {
                fclose(ptr);
                sprintf(filename, "%03i.jpg", indx);
                ptr = fopen(filename, "w");
                fwrite(buffer, 1, BLOCK_SIZE, ptr);
            }
        }
        else
        {
            fwrite(buffer, 1, BLOCK_SIZE, ptr);
        }
    }

    return 0;
}

我认为我理解它应该如何工作,并尝试实现来自步骤说明的伪代码,但我面临的问题是我不知道如何访问文件指针。我的意思是,如果我找到新文件,然后我说ptr = fopen并在那里写入,但是当我找到下一个文件时,我无法关闭前一个文件,同样也适用于else语句,当我找不到新文件时 - 我无法写入已经打开的文件,因为它在另一个if语句中。

你能给我一些建议吗?

英文:

Thats's my current code.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdint.h&gt;

typedef uint8_t BYTE;
int const BLOCK_SIZE = 512;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf(&quot;Incorrect usage\n&quot;);
        return 1;
    }

    FILE *file = fopen(argv[1], &quot;r&quot;);
    if (file == NULL)
    {
        printf(&quot;Could not open the file\n&quot;);
        return 1;
    }

    BYTE buffer[512];
    int indx = 0;
    int irratation= 0 ;
    char* filename = malloc(10);

    while(fread(buffer, 1, BLOCK_SIZE, file) == BLOCK_SIZE)
    {
        FILE* ptr = NULL;

        if (buffer[0] == 0xff &amp;&amp; buffer[1] == 0xd8 &amp;&amp; buffer[2] == 0xff &amp;&amp; (buffer[3] &amp; 0xf0) == 0xe0)
        {
            irratation++;
            if(irratation &gt;= 2)
            {
                indx++;
            }

            if (indx == 0)
            {
                sprintf(filename, &quot;%03i.jpg&quot;, indx);
                ptr = fopen(filename, &quot;w&quot;);
                fwrite(buffer, 1, BLOCK_SIZE, ptr);
            }
            else
            {
                fclose(ptr);
                sprintf(filename, &quot;%03i.jpg&quot;, indx);
                ptr = fopen(filename, &quot;w&quot;);
                fwrite(buffer, 1, BLOCK_SIZE, ptr);
            }
        }
        else
        {
            fwrite(buffer, 1, BLOCK_SIZE, ptr);
        }
    }

    return 0;
}

I think I understand how it should work and I try to implement pseudocode from walkthrough, but the problem I am facing is that I don't know how to access the file pointer. I mean if I find new file, then I say that ptr = fopen and I write there, but when I found next one, I can't close previous one, and also the same goes with else statement, when I don't find new file - I can't write to a file that I have already opened as it's in another if statement.

Can you guys give me some advice on that?

答案1

得分: 0

首先,这些图像由多个大小超过512的块组成,你正在做的是将每512字节写入一个新文件。

其次,"irratation" 变量代表什么?如果你考虑一下,每当在 if 语句中找到一个 jpeg 前缀时,你可以递增索引,这将没问题。但你现在的做法是(在修复文件问题后),"irratation" 首先设置为0,在 while 条件中,fread 读取512字节,"irratation" 增加一次而 "index" 不变。"index" 仍然为零,它找到 jpeg 前缀并写入一个新文件。然后循环重新开始,想一想会发生什么,"irratation" 真的有什么作用吗?

还有一个小问题,你的文件名是 "###.jpg",你能再计算一次需要多少空间吗?不要忘记 '\0'!

考虑到 CS50 学术诚信和规则,我尽量提供了尽可能多的提示,所以请尝试弄清楚这些问题!

英文:

First of all, the images are made of more than one block size of 512, what you're doing is writing every 512 bytes into a new file. <br>
And secondly, what does the irratation variable stand for. If you think about it, everytime you find a jpeg prefix that you're checking in the if statement, you can increment the index, and it would be fine, what you're doing is that (after you fix the file problem) irratation is first set to 0, in the while condition, fread reads 512 bytes, irratation is incremented by one and index is not. Index is at zero, it finds the jpeg prefix and writes in a new file. The loop now restarts, and think about what happens, is the irratation truly doing anything?<br>
Another minor thing, your filename is "###.jpg" can you count another time how much space do you need? Don't forget the '\0'!
I tried to just hint as much as I could considering the CS50 Academic Honesty and rules, so try to figure that out!

huangapple
  • 本文由 发表于 2023年7月17日 19:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定