My program keeps SEGFAULTing

+2 Devin McElheran · February 15, 2015
I've written a program to copy a file and recreate it with the same name but with .bk appended. It's an exercise to get a grasp on the fread and fwrite functions.


//fbk.c - Creates backup of file in the form of .bk
//Usage: fbk ...

#include
#include
#include
#include       //Standard System Exit Codes

#define EXT ".bk"


int main(int argc, char *argv[])
{
    FILE * source;
    FILE * bkup;
    char   temp[256];
    int    bread;             //number of bytes read using fread
    char   *buffer = malloc(512);       //512 byte buffer


    int i = 0;              //Loop Iteration

    if (argc < 2)
        {
            fprintf(stderr, "Command Usage: fbk ...\n");
            exit(EX_USAGE); //Return error code for improper command usage
        }

    for (i = 1; i < (argc - 1); i++)
        {
            strncpy(temp, argv[i], sizeof(temp));

            if ((source = fopen( argv[i], "r")) == NULL)
                {
                    fprintf(stderr, "File %s does not exist.\n");
                }
            else
                {
                    if (fopen( strncat(temp, EXT, (sizeof(temp) - strlen(EXT))), "a") == NULL)
                        {
                            fprintf(stderr, "Could not create file %s.\n", temp);
                            exit(EX_CANTCREAT);
                        }
                    else
                        {
                            while (!feof(source))
                                {
                                    if ((bread = fread(buffer, sizeof(char), sizeof(buffer),source)) == sizeof(buffer))
                                        {
                                            if (ferror(source) != 0)
                                                {
                                                    fprintf(stderr, "Could not read file %s.\n", argv[i]);
                                                    exit(EX_IOERR);
                                                }
                                        }

                                    if ((bread = fwrite(buffer, sizeof(char), bread, bkup) != bread))
                                        {
                                            fprintf(stderr, "Could not write to file.\n");
                                            exit(EX_IOERR);
                                        }
                                }
                        }
                }

        fprintf(stdout, "Completed %s backup to %s.\n", argv[i], temp);

        }
}




It keeps giving me a segmentation fault on the fwrite call. I thought it was to do with my buffer, so I changed it from a char array to a pointer to char and associated it with a block of 512 bytes using malloc. But I don't know why it's faulting. I'm getting the impression it's not buffer causing it.

Post a Reply

Replies

Oldest  Newest  Rating
0 Devin McElheran · February 16, 2015
Wow, as if I overlooked that. Thank you. It was that simple, I just put the (bkup =....) in there and it worked. Thanks a lot.
  • 1

C

107,337 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator