【问题标题】:pset4 cs50 recover.c guidancepset4 cs50 recover.c 指导
【发布时间】:2016-11-28 04:58:36
【问题描述】:

我可以使用 cs50 中的这个问题集的一些帮助。每当我的代码遇到 while 循环时,fread 函数就会返回 0。我似乎无法理解为什么会发生这种情况。甚至在我遇到这个问题之前,我的代码并没有按我的意愿工作,所以如果有任何额外的提示,那真的很有帮助。

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

//PROTOTYPES 
bool check(uint8_t block[]);

int main(int argc, char* argv[])
{
    FILE* inptr = fopen("card.raw", "r"); //open up the card.raw file for reading

    if(inptr == NULL)
    {
        fclose(inptr);
        printf("Could not open the card data.\n");
        return 1;
    }

    FILE* outptr;

    int jpegCount = 0;
    bool foundFirstJpg = false;// flag to denote wheather the first jpg has been found

    char str[10]; //to hold the jpg name
    sprintf(str, "%i.jpg", jpegCount); //puts the image name in str
    jpegCount ++;
    outptr = fopen(str, "w"); //Create a new jpeg for each image

    uint8_t block[512]; // a tempory block to hold 512 bytes of info 

    while(fread(block, sizeof(block), 1, inptr) == 1) //read 512 bytes of info at a time from the inptr)
    {
        printf("hello\n");
        if(check(block)) //if I have found a jpg
        {
            if(foundFirstJpg) //if its not my first jpg
            {
                fclose(outptr); //close the previous jpg file
                sprintf(str, "%i.jpg", jpegCount); //puts the image name in str
                outptr = fopen(str, "w"); //Create a new jpeg for each image
                jpegCount ++;
                fwrite(block, sizeof(block), 1, outptr); //write the block to the image file 
            }
            else
            {
                fwrite(block, sizeof(block), 1, outptr); //write first block to the image file 
                foundFirstJpg = true; 
            }
        }
        else //if this is not a jpg
        {
            if(foundFirstJpg) //check if we have found our first jpg
            {
                 fwrite(block, sizeof(block), 1, outptr); //write 512 bytes to the current image file
            }
        }
    } 
    if (outptr)
    {
         fclose(outptr);
    }
    fclose(inptr);
    return 0;
}

//function to check if this is the start or end of a jpg
bool check(uint8_t block[])
{
    bool isJpg = true; //boolean value to be returned
    if (block[0] != 0xff || block[1] != 0xd8 || block[2] != 0xff) //checks if the first 3 bytes are those that represent a jpg
    {
        isJpg = false;
    }
    if (block[3] < 0xe0 || block[3] > 0xef) //checks if the fourth byte also represents a jpg
    {
        isJpg = false;
    }
    return isJpg;
}

【问题讨论】:

    标签: c file io recover cs50


    【解决方案1】:

    您应该以二进制模式打开这些文件:

    FILE *inptr = fopen("card.raw", "rb");
    ...
    outptr = fopen(str, "wb");
    

    如果card.raw 少于512 字节,fread 将返回0

    【讨论】:

    • 不幸的是,这并没有解决我的问题。谢谢你的建议。
    • 另外,当我运行 'xxd -l 2400 card.raw' 尝试从 card.raw 中读取一些字节时,看看是否能得到任何东西。什么都没有出现。我的命令行只是移动到下一行。
    • 如果没有card.raw 文件的副本,很难判断发生了什么。
    • 我需要先道歉。我很抱歉浪费了你的时间来帮助我。我偷偷怀疑 card.raw 有问题。原来它是空的,0字节。我一定是在我的程序的早期版本中以某种方式清空了它。重新下载后,我的程序运行良好。只要我不熟悉我正在使用的 ide,我就花了我很长时间,因此在调查文件时遇到了麻烦。再次感谢您的帮助!
    • 我猜我的提示是正确的:如果 card.raw 的字节数少于 512,fread 将返回 0。.
    猜你喜欢
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多