fgets crash issues

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 14
Reputation: Nemes is an unknown quantity at this point 
Solved Threads: 0
Nemes Nemes is offline Offline
Newbie Poster

fgets crash issues

 
0
  #1
Nov 27th, 2007
Hello, I am running a heuristic search algorithm (running on Windows XP), and at each iteration, I want to write results in an output file. The issue that I have is that after a number of iteration, my program crashes at the line where I have the "fputs(str, f);" (See below). It crashes right after it calls the fputs, so I can't even use the returned value of the call for error checking, since it doesn't even seem to get far enough to return a value.

It seems to be dependent on the amount of data that I am writing in the output file. Since I have multiple test problems that I can run my algorithm on, I can test it on several different problems. While the numbers are not constant, the program is usually able to write roughly 120 lines, or 5400 characters, no matter the test problem that I use.
Here is the portion of the code where I write the data onto the file.

  1. FILE * f = fopen("Output.txt", "a");
  2. sprintf(str, "%lf,%lf,%d,%d,%lf,%lf\n\0", var1, var2, var3, var4, var5, var6)
  3. fputs(str, f);
  4. fclose(f);


Also, the resulting string 'str' has no problematic values, because I can put the line "printf("%s", str);" before the fputs() line, and it will show the value of the string properly.

Since I don't even have a clue as to why a fgets() call crashes my problem, I am turning to you guys, hoping you know what can cause this.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 14
Reputation: Nemes is an unknown quantity at this point 
Solved Threads: 0
Nemes Nemes is offline Offline
Newbie Poster

Re: fgets crash issues

 
0
  #2
Nov 27th, 2007
Well, nevermind, I feel stupid now... It seems I had a "file leak" somewhere else in my program that opened a file and never closed it, so I attained the maximum number of files open.

I had to test it on a Linux system, though, to give me the errno. Any reason why on Windows the program doesn't return an error value when it can't open a file?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,519
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: fgets crash issues

 
0
  #3
Nov 27th, 2007
on MS-Windows fopen() returns NULL on error, which it should do on all other operating systems.
Last edited by Ancient Dragon; Nov 27th, 2007 at 8:00 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC