RSS Forums RSS

Assertion Failure???

Please support our C++ advertiser: Programming Forums
Reply
Posts: 3
Reputation: ogi1989 is an unknown quantity at this point 
Solved Threads: 0
ogi1989 ogi1989 is offline Offline
Newbie Poster

Assertion Failure???

  #1  
Dec 10th, 2008
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?
  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 8:05 am. Reason: added code tags
AddThis Social Bookmark Button
Reply With Quote  
Posts: 665
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 107
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Assertion Failure???

  #2  
Dec 10th, 2008
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 6:36 am.
Knowledge is power -- But experience is everything
Reply With Quote  
Posts: 7,460
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Assertion Failure???

  #3  
Dec 10th, 2008
>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:
  1. fp = fopen ( filename, "r" );
  2.  
  3. if ( fp == NULL ) {
  4. perror ( "Error opening file" );
  5. exit ( EXIT_FAILURE );
  6. }
I'm here to prove you wrong.
Reply With Quote  
Posts: 3
Reputation: ogi1989 is an unknown quantity at this point 
Solved Threads: 0
ogi1989 ogi1989 is offline Offline
Newbie Poster

Re: Assertion Failure???

  #4  
Dec 10th, 2008
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!
Reply With Quote  
Posts: 5,126
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 633
Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Assertion Failure???

  #5  
Dec 10th, 2008
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
Reply With Quote  
Posts: 3
Reputation: ogi1989 is an unknown quantity at this point 
Solved Threads: 0
ogi1989 ogi1989 is offline Offline
Newbie Poster

Re: Assertion Failure???

  #6  
Dec 10th, 2008
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.
Reply With Quote  
Posts: 5,126
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 633
Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Assertion Failure???

  #7  
Dec 10th, 2008
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 664 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:29 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC