943,660 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2162
  • C++ RSS
Dec 10th, 2008
0

Assertion Failure???

Expand Post »
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?
C++ Syntax (Toggle Plain Text)
  1. #include <conio.h>
  2.  
  3. int Artm(char *filename);
  4. int Count(char *filename);
  5.  
  6. int main()
  7. {
  8. char filename[81]="D:\\Test\\sign.txt";
  9. Artm(filename);
  10. Count(filename);
  11. return 0;
  12. }
  13.  
  14. int Artm(char *filename)
  15. {
  16. FILE *fp;
  17. float sr,a,i;
  18. char out[81]="\0";
  19. FILE *fout;
  20.  
  21.  
  22. fp=fopen(filename,"r");
  23. while(fscanf(fp,"%f",&a)!=-1)
  24. {
  25. sr+=a;
  26. i++;
  27. }
  28. sr=sr/i;
  29. printf("Save to:\1.file\n2.screen");
  30. scanf("%f",&a);
  31. switch(int(a))
  32. {
  33. case 1: printf("Filename ?\n");
  34. scanf("%s",out);
  35. fout=fopen(out,"w");
  36. fprintf(fout,"%f",sr);
  37. fclose(fout);
  38. break;
  39. case 2: printf("%f",sr);
  40. break;
  41. default: printf("1 or 2!");
  42. }
  43. fclose(fp);
  44. return 0;
  45. }
  46.  
  47. int Count(char *filename)
  48. {
  49. FILE *fp;
  50. float a,i;
  51.  
  52. fp=fopen(filename,"r");
  53. while(fscanf(fp,"%f",&a)!=-1)
  54. if(a==6)
  55. i++;
  56. printf("Count:%d",i);
  57. fclose(fp);
  58. return 0;
  59. }
Last edited by Narue; Dec 10th, 2008 at 9:05 am. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ogi1989 is offline Offline
3 posts
since Dec 2008
Dec 10th, 2008
0

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
Last edited by Freaky_Chris; Dec 10th, 2008 at 7:36 am.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 10th, 2008
0

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:
C++ Syntax (Toggle Plain Text)
  1. fp = fopen ( filename, "r" );
  2.  
  3. if ( fp == NULL ) {
  4. perror ( "Error opening file" );
  5. exit ( EXIT_FAILURE );
  6. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 10th, 2008
0

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ogi1989 is offline Offline
3 posts
since Dec 2008
Dec 10th, 2008
0

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 10th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ogi1989 is offline Offline
3 posts
since Dec 2008
Dec 10th, 2008
0

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Get path to file found by FindFirstFile
Next Thread in C++ Forum Timeline: Win32API Movefile() Pathname problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC