-
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?
-
Code:
if((spData = fopen("in.text", "r")) = NULL)
Look closely at that line. Not sure if it's directly related to your problem however.
-
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.
-
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);