Problem with fread?

Hi there,

I've got a prob with the following code. I already spend several hours fault finding but couldn't fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.

1
2
3
4
5
  PrepChan = 1;
  OpenFile(filename1);
				  
  PrepChan = 2; 
  OpenFile(filename2);


1
2
3
4
5
6
7
8
9
10
11
12
13
//cout after PrepChan=1, left side norm1, right side norm2
1: 2000.0 - 0.0
1: 4000.0 - 0.0
1: 6000.0 - 0.0
1: 12000.0 - 0.0
1: 24000.0 - 2000.0

//cout after PrepChan=2, left side norm1, right side norm2
2: 30000.0 - 1500.0
2: 4000.0 - 3000.0
2: 6000.0 - 10000.0
2: 12000.0 - 15000.0
2: 24000.0 - 30000.0


As you can see it already writes something into the second array after the first call. And it overwrites the first array call it the second time.

I really can't figure out why.

Can someone please give me a little hint?

Cheers.
Last edited on
The problem is help <= 4 on line 23/37. It means that you write 5 instead of 4 elements, i. e. out of bounds. Change to help < 4. Note < instead of <=
Sorry but this can't be the right way. There are 5 Values to be written so <=4 or <5 is the correct way.

When I put <4 it looks like:
1
2
3
4
5
6
7
8
9
10
11
1: 2000.0 - 0.0
1: 4000.0 - 0.0
1: 6000.0 - 0.0
1: 12000.0 - 0.0
1: 0.0 - 2000.0

2: 2000.0 - 1500.0
2: 4000.0 - 3000.0
2: 6000.0 - 10000.0
2: 12000.0 - 15000.0
2: 0.0 - 2000.0
There are 5 Values to be written

The lines 6 and 7 of your program beg to differ.

It is better to be systematic (use <) and avoid literal magic values (like 4):
1
2
3
4
5
6
const int 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...withconst int = 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.

Thank you guys for your help.
Last edited on

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.


When you declare an array thus:

 
int array[4];


You are declaring an array that has a capacity of 4 items, in this case integers. The indexing of an array starts at index 0, so you have:

array[0]
array[1]
array[2]
array[4]

Not 5 items as you imply. array[5] is out of bounds.
Topic archived. No new replies allowed.