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

    // I got information about what to do with the buffer
    // from this stack exchangehttps://cs50.stackexchange.com/questions/2680/
    //what-data-structure-do-you-use-to-store-the-buffer-in-recover

    //typedef uint8_t  BYTE;
            //{
                //BYTE buffer;
            //}

    int main(int argc, char *argv[])
    {
        // ensure proper usage
        if (argc != 2)
        {
            fprintf(stderr, "You didn't enter two command line arguments\n");
            return 1;
        }

        char *infile = argv[1];

        // open input file
        FILE *inptr = fopen(infile, "r");
        if (inptr == NULL)
        {
            fprintf(stderr, "Could not open %s.\n", infile);
            return 2;
        }

        FILE *card;

        if (fread(&card,1,512,inptr) == 512)
        {
            int filenumber;

        // would I then do something here about freading into the buffer
        // what I just fread into card. I don't understand why I can't just
        // access each part of card like an array


            if (card[0] == 0xff &&
                card[1] == 0xd8 &&
                card[2] == 0xff &&
                (card[3] & 0xf0) == 0xe0
                {
                    filenumber++

                    if filenumber == 1
                    {

                        sprintf(outfile, %i.jpg, filenumber)

                        FILE *outptr = fopen(outfile, "w");
                        if (outptr == NULL)
                        {
                            fclose(inptr);
                            fprintf(stderr, "Could not create %s.\n", outfile);
                            return 3;
                        }

                        fwrite(&card,1,512, outptr);
                    }

                    if filenumber > 1
                    {
                        fclose (outptr);

                        sprintf(outfile, %i.jpg, filenumber)

                        FILE *outptr = fopen(outfile, "w");
                        if (outptr == NULL)
                        {
                            fclose(inptr);
                            fprintf(stderr, "Could not create %s.\n", outfile);
                            return 3;
                        }
                    }
                }
        }

        else if (card[0] =! 0xff &&
            card[1] =! 0xd8 &&
            card[2] =! 0xff &&
            (card[3] & 0xf0) =! 0xe0
        {

            if filenumber > 0
            {
                fwrite(&card,1,512, outptr);
            }
            //else if filenumber is 0 just do nothing, as I am doing
        }
        else
        {
            fclose (inptr);
            fclose (outptr);
        }
    }

I've looked online, and I've read that I need to store the beginning of the JPEG in a buffer, but I'm not sure how to do that. Am I just freading the same thing I put into card into the buffer? What is the point of the buffer and what's wrong with my current implementation?

1

The code is trying to treat the output file like memory. While it may be technically possible (not the way the code is doing it), that is a very inefficient method. Most importantly, file I/O is terribly slow compared to processing data in memory. There are more reasons, but they pale by comparison to this single reason.

A buffer is nothing more than memory storage. In this case, it comes in the form of a char array of appropriate size. (512 bytes seems about right, doesn't it?) Data is copied from the input file, processed in memory, and then written out to the output file. It's that simple - nothing fancy about it, other than calling it a buffer (a place to store something, perhaps repeatedly storing one data block after another, while being processed).

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • So, I think what you're saying is that instead of having card be a file, I want it to be stored in memory because that would be quicker. However, if that is the case, I'm not sure how to implement it – user21860 Aug 15 '18 at 2:07
  • For that, I'll refer you to the class material including the walkthroughs. If you still can't figure it out, leave a comment. – Cliff B Aug 15 '18 at 3:29
  • I stored card in memory using malloc, and was able to debug from there, but ran into several more errors, so I posted a new question – user21860 Aug 15 '18 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .