Seg Fault -- Plz Help Quick

This is a discussion on Seg Fault -- Plz Help Quick within the C Programming forums, part of the General Programming Boards category; i get a stupid seg fault when i try to open/read a file. all works well if the file == ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Unhappy Seg Fault -- Plz Help Quick

    i get a stupid seg fault when i try to open/read a file. all works well if the file == NULL and an error has to be printed, but it either just continuly loops or seg faults if the file is a good one. it usually stops on or around the scanf if i get a seg fault. plz take a look.

    Code:
       FILE *in_file = NULL;                             /* Declare global batch read in file handler */
       char user_file[STR_MAX];                          /* Declare temporary variable to hold user input */
       int goods = 0, bads = 0, eof_test = FALSE;        /* Declare temporary counting variables */
       int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;     /* Declare temporary counting variables */
       printf("\nAutomatic Mode Is Running...\n\n");     /* Display opening message */
       good_file = fopen("address_book.txt", "w");       /* Open file for correct output using write parameter */
       bad_file = fopen("errors.txt", "w");              /* Open file for incorrect output using write parameter */
       printf("Please enter the filename of your address book: >> "); /* Ask user for input file name */
       scanf("%s", &user_file);                          /* Take line of input from user for file name */
       if((in_file = fopen("a.txt", "r")) == NULL)     /* Check if read file is corrupted */
       {                                                 /* Start if statement */
          printf("\nCannot open input file, please check the filename try again.\n\n"); /* Print error message */
          exit(1);                                       /* Exit after input file error */
       }                                                 /* End if statement */
       z = fgetc(in_file);                               /* Gather character from file to temporary variable */
       while(eof_test == FALSE)                          /* Continue read loop while end of file is not found */
       {                                                 /* Start while loop */
          for(a = 0; a < MAX; a++)                       /* Run loop until maximum address book size */
          {                                              /* Start for loop */
             rester();                                   /* Call to reset word test variables */
             for(b = 0; b < UNIT_MAX; b++)               /* Run loop until maximum unit size */
                if((z == ' ') && (start == FALSE))       /* Check if read in character is a space */
                {                                        /* Start if statement */
                   b--;                                  /* Reset counter back one to allow space */
                }                                        /* End if statement */
                else if((z >= '0') && (z <= '9'))        /* Check if first character is a house number */
                {                                        /* Start if statement */
                   fptr->number[a][0] = z;               /* Move read in character into struct */
                   house = TRUE;                         /* Set test for house or unit to true */
                   break;                                /* Break from unit loop if its a house */
                }                                        /* End if statement */
                else                                     /* Else read in character is not a space */
                {                                        /* Start else statement */
                   start = TRUE;                         /* Set started to true if word has begun */
                   if((z == ' ') && (start == TRUE))     /* Check if character is a space and word has started */
                   {                                     /* Start if statement */
                      end = TRUE;                        /* Set end to true if at end of word */
                   }                                     /* End if statement */
                   else                                  /* Else character is letter in word */
                   {                                     /* Start else statement */
                      fptr->flat_unit[a][b] = z;         /* Move read in character into struct */
                   }                                     /* End else statement */
                }                                        /* End else statement */
                z = fgetc(in_file);                      /* Gather character from file to temporary variable */
             }                                           /* End for loop */
    ....
    ....
    ....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    In future, please avoid phrases like "quick" or "urgent" (see the sig)

    > int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
    I see you've continued the habit of having lots of unused and meaningless variable names scattered in your code.
    1. Get rid of the ones you don't need
    2. Give meaningful names to the ones you do need.

    > fptr->flat_unit[a][b] = z;
    Without any idea of how you declared and initialised this, we can't help you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 04:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21