Thread: debug assertion failed

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    Post debug assertion failed

    Code:
    /* This program expects the name of an input file and an output
    file to be given by the user.
          Name : Dang Vo
    	  Date : 04/14/11
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define NUM_LINE 21
    
    typedef struct
    {
    	char states[100];
    	char cities[100];
    	int tempt;
    } INFO;
    void getData(FILE *spData, INFO list[]);  
    void insertionSort(INFO list[], int last);
    
    int main (void)
    {
    
    // Local Declarations
    
    FILE *spData = fopen("in.txt", "r");
    INFO list[NUM_LINE];
    
    
    // Statements
    getData(spData, list);
    
    return 0;
    }
    
    /*========================== getData========================
    Pre: FILE *spData
    Post: FILE *spData 
    */
    // Local Declarations
    int i;  
    
    void getData(FILE *spData, INFO list[NUM_LINE])
    {
    	if((spData = fopen("in.text", "r")) = NULL)
            printf("Error openning in.txt\n");
     
    for (i = 0; i < NUM_LINE; i++)
    fscanf(spData,"%s %s %d", list->states, list->cities, list->tempt );
    
    
    return;
    }
    i have this code here when i try to run it it say debug assertion failed what did i do wrong?

  2. #2
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Code:
    if((spData = fopen("in.text", "r")) = NULL)
    Look closely at that line. Not sure if it's directly related to your problem however.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Debug assertions usually come with some kind of statement as to what failed, usually in the form of a predicate which evaluated to false.

    Say
    assert( str != NULL )

    Aside from the = issue already mentioned, why are you opening the file twice.
    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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok you've created an array of 21 INFO items...

    Code:
    INFO list[NUM_LINE];
    Then you call your function...
    Code:
    void getData(FILE *spData, INFO list[NUM_LINE])
    but you only pass in one item in the array... #21...

    However... You have an array of 21 items. In C arrays start at 0, not 1... so an array with 21 items has valid indexes from 0 to 20... not 1 to 21... thus element 21 is actually out of bounds for the array.

    You need to pass in a pointer...
    Code:
    void getData(FILE* spData,INFO *list);
    Which you would call as...
    Code:
    getData(spData,list);
    then in your loop you load your array like it's a single struct...
    Code:
    fscanf(spData,"%s %s %d", list->states, list->cities, list->tempt );
    In this case #21... which is out of bounds.

    The correct notation is more like...
    Code:
    fscanf(spData,"%99s %99s %d", list[i].states, list[i].cities, list[i].tempt);
    Last edited by CommonTater; 04-15-2011 at 09:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Assertion Failed
    By Freddy92 in forum C++ Programming
    Replies: 4
    Last Post: 01-30-2011, 03:52 PM
  2. debug assertion failed
    By rahulsk1947 in forum C++ Programming
    Replies: 7
    Last Post: 06-14-2009, 03:36 PM
  3. Debug Assertion Failed!
    By IndioDoido in forum C Programming
    Replies: 31
    Last Post: 03-25-2008, 11:07 AM
  4. debug assertion failed?!?
    By ichijoji in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 03:23 PM
  5. Debug assertion failed
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 10-17-2002, 04:34 PM