CS50 Recover作业:生成50个JPEG文件但无法正常打开求助
- 正在完成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

