You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CS50 Recover作业:生成50个JPEG文件但无法正常打开求助

CS50 Recover作业:生成的JPEG无法在VS Code中打开问题排查
  • 正在完成CS50的Recover编程作业,任务是从card.raw中恢复被删除的50个JPEG文件。实现逻辑为:检查每个512字节块的前4字节判断JPEG起始位置,写入新文件;遇到新的JPEG起始时切换文件,直至读取到文件末尾。
  • 代码运行后生成了恰好50个JPEG文件,但在VS Code中打开时出现如下错误:

Error loading webview: Error: Could not register service workers: NotSupportedError: Failed to register a ServiceWorker for scope ('https://0foennjt10in3q0224oduooshmepdo2jsp1eg131ejbmkj7a2i7k.vscode-cdn.net/stable/8fa188b2b301d36553cbc9ce1b0a146ccb93351f/out/vs/workbench/contrib/webview/browser/pre/') with script ('https://0foennjt10in3q0224oduooshmepdo2jsp1eg131ejbmkj7a2i7k.vscode-cdn.net/stable/8fa188b2b301d36553cbc9ce1b0a146ccb93351f/out/vs/workbench/contrib/webview/browser/pre/service-worker.js?v=4&vscode-resource-base-authority=vscode-resource.vscode-cdn.net&remoteAuthority=codespaces+sebdoubleu-code50-111027905-wpxr5g54pv9h9667'): The user denied permission to use Service Worker..

我的C语言代码

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./recover IMAGE\n");
        return 1;
    }

    FILE *forensic = fopen(argv[1], "r");

    if (forensic == NULL)
    {
        printf("File was not found\n");
        return 1;
    }

    FILE *currentFile;
    currentFile = NULL;
    int jpegCount = 1;
    char *filename = NULL;
    filename = malloc(8 * sizeof(char));

    BYTE buffer[512];
    while (fread(buffer, sizeof(char), 512, forensic) != 0)
    {
        // Check for matching 4 bytes, indicating start of a new JPEG
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // If file exists, close it and start a new one
            if (currentFile != NULL)
            {
                fclose(currentFile);
                sprintf(filename, "%03i.jpg", jpegCount);
                currentFile = fopen(filename, "w");
                jpegCount++;
            }
            // If no file exists yet, create one
            else
            {
                sprintf(filename, "%03i.jpg", jpegCount);
                currentFile = fopen(filename, "w");
                jpegCount++;
            }
        }
        // If file exists, write to it
        if (currentFile != NULL)
        {
            BYTE wBuffer[512];
            fwrite(wBuffer, sizeof(BYTE), 512, currentFile);
        }
    }
    fclose(currentFile);
    fclose(forensic);
    free(filename);
    return 0;
}

作为编程新手,可能存在未发现的代码错误,希望得到帮助排查问题。

内容的提问来源于stack exchange,提问作者Robert Paulson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:40:48