GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Apr-2016, 20:08
budgie1980 budgie1980 is offline
New Member
 
Join Date: Feb 2016
Posts: 6
budgie1980 is on a distinguished road

Help with a segmentation fault


Hey everyone, wanted to see if anybody could help me with a segmentation fault. I've ran gdb debugging on it a few times, but I've never really used it before so I don't really know what I'm doing. It does say that I have a seg fault, but I can't find out what it is, or where it is. My code is below. I know it looks like a lot, but if someone could just run a debugging program through it, it would help me out so much. Thanks for you time and help

This program reads in a list of numbers, prints it to stdout, uses
bubble sort to sort it, prints it again, and then writes it to the
specified output file.


CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>


// Function prototypes
void printArray(int array[], int size);
int  readNumbers(int array[], char* fname);
void writeNumbers(int array[], int size, char* fname);
void bubbleSort(int array[], int size, bool isIncreasing);
void sort(int* first, int* second, bool isIncreasing);
void swap(int* first, int* second);


// fill in the rest of the header for the main() function so that
// it takes in command-line arguments
int main(int argc, char *argv[]) {
    const int MAX_FILENAME_LEN = 256;
    const int MAX_NUMBERS      = 100;

    char inFilename[MAX_FILENAME_LEN];
    char outFilename[MAX_FILENAME_LEN];
    int  numbers[MAX_NUMBERS];
    int  count;
    int  exitValue = 1;
        
    // use sscanf() to extract the filename from the argv[] array
         //    and put it into the variable called  inFilename
        printf("Enter the input file name: ");
        sscanf(argv[1], "%s", inFilename);      


    count = readNumbers(numbers, inFilename);

         if (count == 0)
                  printf("The input file was empty.\n\n");
    else if (count > 0) {
        printf("Enter the output file name (will be created/overwitten): ");
        scanf("%s", outFilename);

                  printf("\n");
        printArray(numbers, count);
        bubbleSort(numbers, count, true);

        printArray(numbers, count);
                  printf("\n");
        writeNumbers(numbers, count, outFilename);

        exitValue = 0;
    }

    return exitValue;
}


/*  readNumbers()
    This function tries to open the file whose name is contained in the
    string variable 'fname' and read its contents into the array 'array'.  

    preconditions:
    - 'array' is an integer array large enough to hold all of the
      elements in the file and 'fname' represents a valid filename

    postconditions:
    - If 'fname' can be opened then its contents are read into
      the array 'array', the function returns the number of elements
      read.  If 'fname' is invalid an error message is printed and
      -1 is returned.
*/
int readNumbers(int array[], char* fname) {

    // Declare local variables and FILE pointer
        FILE *myfile1;
        int numberRead=0;
    // Use fopen to try to open the file for reading
        myfile1 = fopen(fname, "r");
    // Test to see if the file was opened correctly
        if(myfile1 == NULL)
        {
                printf("Error, file %s could not be opened\n", fname);
                exit(1);
        }
    // Now read until !feof(...
        while (!feof(myfile1))
        {
                fscanf(myfile1,"%i", &array[numberRead]);
         myfile1 = fopen(fname, "r");
                ++numberRead;
        }
    // Close the file pointer
                fclose(myfile1);
    // Return the number of items read

    return numberRead;
}


/*  writeNumbers()
    This function writes the first 'size' numbers from array 'array' to a file
    whose name is contained in 'fname'.

    preconditions:
    - 'array' is an array of integers equal to or larger than
      size and 'fname' holds a valid filename

    postconditions:
    - If a file can be created then 'size' elements from
      a are written to that file and it is closed.  If
      the file cannot be created then an error message is printed.
*/
void writeNumbers(int array[], int size, char* fname) {
    // Declare local variables and FILE pointer

        FILE *myfile2;
        int i = 0;
    // Use fopen to try to open the file for writing
        myfile2 = fopen(fname, "r");
    // Test to see if the file was opened correctly
        if(myfile2 == NULL)
        {
                printf("Error, file %s could not be opened\n", fname);
                exit(1);
        }
        while(i<size)
        {
    // use fprintf to write to the file
        fprintf(myfile2, "%d", array[1]);
    // close the file
        fclose(myfile2);
        }
}



/*  This function prints the first 'size' elements from the array 'array' to
    stdout using printf

    preconditions:
     - array' is a valid array with >= 'size' elements

    postconditions:
     - Contents of 'array' are printed to standard output
*/
void printArray(int array[], int size) {
    if (size > 0) {
        int i;

        printf("%d", array[0]);

        for (i = 1; i < size; i++) {
            printf(", %d", array[i]);
        }
        printf("\n\n");
    }
}


/*  bubbleSort()
    This function implements the bubble sort algorithm

    preconditions:
    - 'array' is an array with at least 'size' elements

    postconditions:
    - If isIncreasing is true, then the array will be sorted
      into non-decreasing order.  If it is false,
      then the array will be sorted into non-increasing order
*/
void bubbleSort(int array[], int size, bool isIncreasing) {
    if (size >= 2) {
        int i;

        for (i = 0; i < size; i++) {
            int j;

            for (j = 0; j < size - 1 - i; j++) {
                sort(&array[j], &array[j + 1], isIncreasing);
            }
        }
    }
}


/*  sort()
    Sort will interchange the values pointed to by 'first' and 'second' if
    they aren't in the correct order as specified by 'dir'

    preconditions:
     - integer pointers 'first' and 'second' are non-NULL

    postconditions:
     - If isIncreasing is true then 'second' will be >= 'first'
     - If isIncreasing is false then 'second' will be <= 'first'
*/
void sort(int* first, int* second, bool isIncreasing) {
    if (  ( (!isIncreasing) && (*first < *second) ) ||
                    ( ( isIncreasing) && (*first > *second) )   ) {
        swap(first, second);
    }
}


/*  swap()
    Swaps the values pointed to by the parameters 'first' and 'second'

    preconditions:
     - Neither 'first' nor 'second' are NULL

    postconditions:
     - The variable pointed to by 'first' has the initial value of
       the variable pointed to by 'second', and vice versa.
*/
void swap(int* first, int* second) {
    int holder;

    holder  = *first;
    *first  = *second;
    *second = holder;

}
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can anyone help with this segmentation fault budgie1980 C Programming Language 3 11-Feb-2016 20:27
Runs OK but segmentation fault error after Lucas Masalskas C Programming Language 4 01-Jun-2012 16:36
A weird segmentation fault error uwowizard C Programming Language 2 19-Dec-2011 11:32
Debugging a segmentation fault theplankt C++ Forum 1 01-Dec-2011 16:55
child pid xxx exit signal Segmentation fault (11) bezak Apache Web Server Forum 1 24-Nov-2003 10:18

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:51.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.