简体   繁体   中英

reading from a file inside fread() while loop

I am working on problem set 4 "Memory" and trying to understand fread() function and how using fread() inside of a while loop of fread() works. I am trying to read a file until end of file, which is what my while loop is for, then when I find the JPEG file signature, I would like to read from that file until I find the next JPEG signature. My question is how does this work for the two separate calls to fread() function? Once I find the file signature and start reading from file using fread() inside the while loop then iterate over that while loop after exiting my if conditional, does the while(fread()) pickup where fread() left off inside the while loop? Please see code below:

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

// using keyword typedef to give the uint8_t a new name of BYTE (capitalized by convention)
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    // must be two arguments or program not run correctly
    if(argc != 2)
    {
        printf("Usage: ./recover filename\n");
        return 1;
    }

// open file and store its location in a pointer called infile
FILE *infile = fopen(argv[1], "r");
if(infile == NULL)
{
    printf("file cannot be opened or doesnt exist...\n");
    return 1;
}

// read using fread(), each 512 byte block into a buffer
//need a buffer of size 512 BYTEs
BYTE buffer[512];
while(fread(&buffer, sizeof(BYTE), 512, infile))
{
    // create buffer to store a filename with a formatted string of ###.jpg starting at 000.jpg
    int number = 0;
    char filename[8];

    sprintf(filename, "%03i.jpg", number);

    // this demarks a JPEG using bitwise logical & for last buffer bit of signature
    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        
        // this will be the first signature read and start of a JPEG
        if(number == 0)
        {
        // open new file
        FILE *img = fopen(filename, "w");
        // write what is currently in buffer into file
        fwrite(&buffer, sizeof(BYTE), 512, img);
        
        // continue reading from file where left off and write it to img file until a new file signature?
        while(fread(&buffer, sizeof(BYTE), 512, infile))
        {
            fwrite(&buffer, sizeof(BYTE), 512, img)
            if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
            {
                break;
            }
        }

My question is about the second call to fread() while inside the while loop and how that call to fread() affects the call to fread() for the initial while loop. Hope this made sense.

I think you have figured out the solution in the comments. You can also use functions and recursion here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM