Question

Hey I've tried a lot of programs in Visual Studio and in most of them when i try taking the input from a stream ( while using fscanf ) it invariably throws a debug assertion failed error ..

and goes on to say:

stream != NULL. Since I have gotten this error a number of times .. I assume there is a flaw in the way I'm using fscanf. I would appreciate it if someone could tell me the usage or .. give me a demo sample code that illustrates the simple usage .. !

I tried looking up the error .. in most places it said I haven't closed the file .. but I have and I'm a little confused .. I appreciate any help .. thanks a lot :)

    printf("Enter No of states\n");
     Q=5;
  //  scanf("%d",&Q);

   // READING ZERO MATRIX
  // reading the matrix from f0.sta
 {
  FILE *fp;
   fp = fopen("c:\\tc\\fuzzy\\f0.sta","r");
   for(i=1;i<=Q;i++)
    for(j=1;j<=Q;j++)
     fscanf(fp,"%f",&a0[i][j]);

    fclose(fp);
 }
 // READING ONE MATRIX
 // reading the matrix from f0.sta
   FILE *fp;
    fp = fopen("c:\\tc\\fuzzy\\f1.sta","r");
     for(i=1;i<=Q;i++)
      for(j=1;j<=Q;j++)
        fscanf(fp,"%f",&a1[i][j]);

    fclose(fp);

This is the code bit.

Was it helpful?

Solution

It sounds like fp is NULL. The most likely reason is that one of the files (or both) do not exist, or can't be opened (for example, because some other process is using it).

I would start by adding some error checking after the two fopen() calls: compare the result to NULL and if it is NULL, examine errno.

OTHER TIPS

Your loop counters start at 1 instead of 0, which is odd for C programming. What's likely happening is you're not allocating enough space in the array, i.e. you have

double a[5][5];

when you need

double a[6][6];

so you're stepping on something past the end of the array. Better to have your loop be

for(i=0;i<Q;i++)
  for(j=0;j<Q;j++)

so you don't waste the 0 slots in the array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top