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.

FILE * f = fopen("Output.txt", "a");
sprintf(str, "%lf,%lf,%d,%d,%lf,%lf\n\0", var1, var2, var3, var4, var5, var6)
fputs(str, 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.


Recommended Answers

All 2 Replies

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?

on MS-Windows fopen() returns NULL on error, which it should do on all other operating systems.

Be a part of the DaniWeb community

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