struct Std
{
float sp;
float sv;
};
Std norm1[4];
Std norm2[4];
int OpenFile(char* filename)
{
FILE *fp;
Std data;
fopen_s(&fp, filename, "rb");
if (fp == NULL)
{
Error0004(); //file does not exist
return(2);
}
if (PrepChan == 1)
{
for (int help = 0; help <= 4; help++)
{
if (fread(&data, sizeof(data), 1, fp) < 1)
{
Error0005(); //file empty
return(3);
}
norm1[help].sp = data.sp;
norm1[help].sv = data.sv;
}
}
if (PrepChan == 2)
{
for (int help = 0; help <= 4; help++)
{
if (fread(&data, sizeof(data), 1, fp) < 1)
{
Error0005(); //file empty
return(3);
}
norm2[help].sp = data.sp;
norm2[help].sv = data.sv;
}
}
fclose(fp);
return(1);
}
If I call the function once for either channel1 or channel2 it works just fine.
It writes all data correctly into the arrays norm1 or norm2. And I can carry on with my Program.
But if I try to call it for both channels at the "same time" it gets messy.
It is better to be systematic (use <) and avoid literal magic values (like 4):
1 2 3 4 5 6
constint ELEM = 42; // named constant. Numeric value occurs exactly once
int foo [ELEM];
for ( int bar=0; bar < ELEM; ++bar ) {
// have fun with foo[bar]
}
Wow...withconstint = 5 it works perfectly.
But I don't really get why. I always thought when I create an Array like Array[4] it gives me an Array with the "boxes" 0,1,2,3,4. So 5 "boxes" that I can use.
[edit]
Ah god...what an Idiot I am. Got it all mixed up. Of course it must be [5] to get the "boxes" 0,1,2,3,4.