I assume cfexist checks to see if a file exists.
First, you are using strcat incorrectly. It performs string concatenation, and it takes a pointer to a char, and a constant pointer to a char. Consider this:
Code: Select all
char *strcat( char *strDestination, const char *strSource );
A valid use of this function would be as follows:
<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>
char szBuffer[256]={0};
strcat(szBuffer, "hello");
strcat(szBuffer, " world");
printf(szBuffer); // displays "hello world"
</pre><HR></BLOCKQUOTE>
Because you are passing a constant as the first argument, say, "game0", the program automatically allocates enough memory to hold "game0" in memory and retrieves the address to give to strcat. However, strcat's first parameter is the szDestination parameter, and the memory allocated was for a constant. When strcat attempts to write to the address, that is when you get your GPF.
Now for your second call, which is strcat(i, ".mid"). What you are trying to do here is to write ".mid" to the memory address 0x00000005. This memory is not yours to touch.
Now I am going to imagine that you are trying to see if game01.mid, game02.mid, game03.mid and such exists. There is a simpler way to do it.
<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>
j = 5;
while(j<10){
char szFilename[260]={0};
sprintf(szFileName, "game0%d.mid", j);
if (cfexist(szFileName)){
sprintf(Songs[j] .filename, szFileName);
sprintf(Songs[j] .melodic_bank_file, "d2melod.bnk");
sprintf(Songs[j] .drum_bank_file, "d2drums.bnk");
j++;
} else {
break;
}
}
if (!(j<10)){
while(j>9){
char szFilename[260]={0};
sprintf(szFileName, "game0%d.mid", j);
if (cfexist(szFileName)){
sprintf(Songs[j] .filename, strcat("game",strcat(i, ".mid")));
sprintf(Songs[j] .melodic_bank_file, "d2melod.bnk");
sprintf(Songs[j] .drum_bank_file, "d2drums.bnk");
j++;
}
else {
break;
}
}
}
</pre><HR></BLOCKQUOTE>
Keep in mind that this code will only write game00.mid to game09.mid. If you have 10, then it will write game010.mid. Alternatively, your call to sprintf could be like this:
<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>
sprintf(szFileName,"game%d%d.mid",
( j >= 10 ? (int) ( j / 10 ) : '0' ),
( j >= 10 ? (int) ( j % 10 ) : j ) );
</pre><HR></BLOCKQUOTE>
Therefore if j = 27, j / 10 would be 2, and j % 10 would be 7.
Good luck.
Matt / SolidAir