DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Assertion Failure??? (http://www.daniweb.com/forums/thread161604.html)

ogi1989 Dec 10th, 2008 6:56 am
Assertion Failure???
 
I'm trying to read from a text file.I think that everything is correct!It gives me this error message:

Debug Assertion Failed!

Program: D:\Test\sign.exe
File: fscanf.c

Expression: stream != NULL

For information on how your program can cause assertion failure, see the Visual C++ documentation on asserts.
CAN SOMEONE HELP?
#include <conio.h>

int Artm(char *filename);
int Count(char *filename);

int main()
{
        char filename[81]="D:\\Test\\sign.txt";
        Artm(filename);
        Count(filename);
        return 0;
}

int Artm(char *filename)
{
        FILE *fp;
        float sr,a,i;
        char out[81]="\0";
                FILE *fout;
       

        fp=fopen(filename,"r");
        while(fscanf(fp,"%f",&a)!=-1)
        {
                sr+=a;
                i++;
        }
        sr=sr/i;
        printf("Save to:\1.file\n2.screen");
        scanf("%f",&a);
        switch(int(a))
        {
        case 1: printf("Filename ?\n");
                scanf("%s",out);
                fout=fopen(out,"w");
                fprintf(fout,"%f",sr);
                fclose(fout);
                break;
        case 2: printf("%f",sr);
                break;
        default: printf("1 or 2!");
        }
        fclose(fp);
return 0;
}

int Count(char *filename)
{
        FILE *fp;
        float a,i;       

        fp=fopen(filename,"r");
        while(fscanf(fp,"%f",&a)!=-1)
                if(a==6)
                i++;
        printf("Count:%d",i);
        fclose(fp);
return 0;
}

Freaky_Chris Dec 10th, 2008 7:35 am
Re: Assertion Failure???
 
I strongly adivse you use code tags [code=c][/code] Also this looks like C code to me...

So perhaps you should post in the C forum.

Chris

Narue Dec 10th, 2008 9:07 am
Re: Assertion Failure???
 
>Expression: stream != NULL
Yet more proof (as if we needed any) that checking for success is a good thing. perror is your friend, my friend:
fp = fopen ( filename, "r" );

if ( fp == NULL ) {
  perror ( "Error opening file" );
  exit ( EXIT_FAILURE );
}

ogi1989 Dec 10th, 2008 10:22 am
Re: Assertion Failure???
 
thank you.Now the program is running but it doesn't work correct.Can you give me some advises how to do it.There is a file with numbers(one number per row) the program has to printf the average value of this numbers and how many times there is "6" between them.Thank you!
It must have 2 options.To write the information in file, or to printf on the screen!

Salem Dec 10th, 2008 1:53 pm
Re: Assertion Failure???
 
So what isn't working now?

The counting?
The average?
The output stream choice?

Here's a cookie
> float a,i;
What's i initialised to?

Then read the manual page on how to use printf, and what type %d is for.

ogi1989 Dec 10th, 2008 1:56 pm
Re: Assertion Failure???
 
the average and the function that count for number 6.I know when we use %d %f or etc.I just can`t find where i`m wrong.It has 0 errors and 0 warnings but it doesn`t work properly.

Salem Dec 10th, 2008 2:01 pm
Re: Assertion Failure???
 
Right-click on the left margin at the first line in main(), and insert break point.

Then do run->debug program.

Then hover over the { } icons which appear to figure out which is "step into", "step over", "step out" etc.

Use these to step through your code one line at a time.
At every step, examine your variables to see if they contain what you expect.

When actuality != expectation, that's a BUG.


All times are GMT -4. The time now is 2:38 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC