NXC: strange behaviour storing numbers to a file
Posted: 11 Feb 2011, 18:30
I have a problem storing 512 value to a file:
1st question:
if I have 512 ints to store then my declarated file size for CreateFile() should be 512*2 plus a little more for eof() etc, or am I wrong?
2nd question:
my array length is 512
although my counter should count from 0...512 writing all array values to a file it keeps on counting up to several millions or so and doesn't stop when 512 has been reached -
what's wrong with my code?
1st question:
if I have 512 ints to store then my declarated file size for CreateFile() should be 512*2 plus a little more for eof() etc, or am I wrong?
2nd question:
my array length is 512
although my counter should count from 0...512 writing all array values to a file it keeps on counting up to several millions or so and doesn't stop when 512 has been reached -
what's wrong with my code?
Code: Select all
#define MaxRecLen 512
int SoundRecord[MaxRecLen]; // detection: 0...MaxRecLen
//**************************************************************************
// File I/O
//**************************************************************************
string sFileName = "FILE001.dat";
void SoundSave() {
unsigned int nFileSize = 16+(MaxRecLen*2); // nFileSize = number_of_integers*2 plus a little more
byte fHandle, i;
int IOresult, counter=0;
int ibuf;
ClearScreen();
TextOut( 0,48, "SpeechRecogn "+version);
PlayTones(Buzz);
DeleteFile(sFileName);
IOresult = CreateFile(sFileName, nFileSize, fHandle);
if (IOresult == LDR_SUCCESS) {
ClearScreen();
for (i=0; i<512; i++) {
counter+=1;
ibuf= SoundRecord[i];
printf1( 0, 24, "counter =%5d", counter);
printf1( 0, 16, "loudness=%5d", ibuf);
WriteLn(fHandle, ibuf);
}
CloseFile(fHandle);
PlayTones(ChordUp);
}
else
PlayTones(sdError);
while(!btnhit());
ClearScreen();
}
// task main() {
// *SNIP*
// SoundSave();
// *SNIP*
}