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;
}

Recommended Answers

All 6 Replies

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

>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 );
}

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!

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.