Problems with WritePrivateProfileString

Hello,
I am reading values from an array and storing them in an init file using WritePrivateProfileString, all values are getting saved to file except for the value at the first index(0) of the array.However, when stepping through in debug the value is getting wrote to the file but once all values are wrote it is getting overwrote to "0" somehow. I have no clue on what is going on. Below is a sample of my code.

[code]
char final[4];
CString field = "";
for(int i = 0; i< 80; i++)
{
temp = table[i].num;
itoa(temp, final, 10);
field.Format("NPG_%d"), i);
WritePrivateProfileString(Section, field, final, PathName)
}
[code]
I don't see anythig wrong with the code I have. By any chance has anyone encountered this before. Any help/advice will be greatly appreciated.
what, and how big is your table array? Because you're going round that loop 80 times regardless of how big your table array is..
Last edited on
Mutexe,
CNpgTable table[80];
CNpgTable is a struct that is defined
Never seen your problem.

And the problem is non-repro for me with this toy program (I replaced the MFC stuff with standard C). But I used my old Windows XP machine, so I suppose it just might be a new bug???

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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>

int main() {
	struct CNpgTable // define pretend struct
	{
		int num;
	};

	CNpgTable table[80];

	for(int i = 0; i< 80; i++) // init elems with 1, 2, 3, ... 80
	{
		table[i].num = (i + 1);
	}

	const char PathName[] = "C:\\Test\\tnjgraham.ini"; // more pretending
	const char Section[] = "cplusplus";

	char final[4];
	char field[64] = ""; // replaced MFC with standard C (sorry about magic num)

	for(int i = 0; i< 80; i++)
	{
		int temp = table[i].num;// assumed temp is an int
		itoa(temp, final, 10);
		sprintf(field, "NPG_%d", i); // replaced MFC with standard C
		WritePrivateProfileString(Section, field, final, PathName);
	}

	// get and display contents of file...

	char Buffer[10 * 1024] = {0};
	int CharsGot = GetPrivateProfileSection(Section, Buffer, _countof(Buffer), PathName);

	const char* pLine = Buffer;
	while('\0' != *pLine)
	{
		printf("%s\n", pLine);
		pLine += (strlen(pLine) + 1);
	}

	return 0;
}


The output ini file is

[cplusplus]
NPG_0=1
NPG_1=2
NPG_2=3
; etc
NPG_77=78
NPG_78=79
NPG_79=80


and the results displayed on the console are

NPG_0=1
NPG_1=2
NPG_2=3
; etc
NPG_77=78
NPG_78=79
NPG_79=80


Andy
Last edited on
Topic archived. No new replies allowed.