0

I am getting seg fault over and over again. Could anyone help out please. I am pretty sure the functional part of the code, the while loop, should be working properly.

Thanks in advance.


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

typedef uint8_t BYTE;


int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: copy infile\n");
        return 1;
    }


    char* infile = argv[1];// create pointer to data file
    FILE *inptr = fopen (infile, "r");//open input file


    if (inptr == NULL) //if the input file cannot be opened
    {
        fprintf(stderr, "Could not open file\n");
        return 2;
    }

    //Declare buffer variable
     unsigned char buffer[512];

    // open output file
    FILE *outptr = NULL;


    //to keep count of the number of times 512 byte chunks have been read.
    int count = 0;



        while (fread(buffer, 1, 512, inptr) == 512)//every chunk should be 512 bytes. If it is less the file is not read at al.
        {
            if (outptr !=NULL)//if the file is opened then continute writing from previous loop
              {

                fwrite (buffer, 1, 512, outptr);

              }


            if (buffer[0] == 0xff &&
                        buffer[1] == 0xd8 &&
                        buffer[2] == 0xff &&
                        (buffer[3] & 0xf0) == 0xe0)
            {
                        if (outptr !=NULL)
                    {
                        fclose(outptr);
                    }
                        else//file not opened yet -
                    {
                        count++;
                        char filename [8];
                        sprintf (filename, "00%i.jpeg", count);
                        outptr = fopen (filename, "w");
                        fwrite (buffer, 1, 512, outptr);
                    }
            }
        }


    // success
    return 0;
}
1

This else block will execute exactly once (providing a jpeg sig is found in the input file):

else//file not opened yet -
                {
                    count++;
                    char filename [8];
                    sprintf (filename, "00%i.jpeg", count);
                    outptr = fopen (filename, "w");
                    fwrite (buffer, 1, 512, outptr);
                }

outptr will never be NULL again after this outptr = fopen (filename, "w"); executes.

This

char filename [8];
sprintf (filename, "00%i.jpeg", count);

is a memory violation that will cause unexpected results. (filename needs 9 bytes to accomodate the sprintf format.)

Be sure the review the spec Hints:

Your program should number the files it outputs by naming each ###.jpg, where ### is three-digit decimal number from 000 on up.

The sprintf format is not going to give the expected result; review this post for guide to correct format.

You also need to think about the consequence of writing a block before you know whether it is a sig block.

1
  • Absolutely. I (with another expert programmer) figured out the problems in my code. Thanks for your valid suggestions. – Indu Dec 17 '19 at 1:25

You must log in to answer this question.

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