C ++ Seekp多次

时间:2016-01-29 13:35:56

标签: c++ arrays for-loop

代码需要通过使用seekp转到特定地址来在文件中写入一些值。但是当我启动它时,只有第一个数组写入文件。 可以像新值一样从INI中读取新地址,但不会将其写入文件中。它用于游戏保存文件。

代码:

const int noq = 5; 
const int noqd = 4; 
int questData[noqd] = {10, 10, 10, 10};
int adresslist[noqd] ={0x0,0x0,0x0, 0x0}; 
fstream aus("quest1", ios::binary | ios::out | ios::in);   
char buffer[512];
int i;
int j;
int Money, Prize, HRpoints, Map;    
int Mappointer, Moneypointer, PrizePointer, HRpointer;


for (j=0; j <= noq; j++)
{
    sprintf_s(buffer,"%d",j);
    Money = GetPrivateProfileInt(buffer,"RewardMoney", 0, ".\\Settings.ini");
    Prize = GetPrivateProfileInt(buffer,"Questprize", 0 , ".\\Settings.ini");
    HRpoints = GetPrivateProfileInt(buffer,"RewardHRpoints", 0 , ".\\Settings.ini");
    Map = GetPrivateProfileInt(buffer,"Map", 1 , ".\\Settings.ini");


    questData[0] = Money;
    questData[1] = HRpoints;
    questData[2] = Prize;
    questData[3] = Map;

    Moneypointer = GetPrivateProfileInt(buffer,"RewardMoney", 0, ".\\Pointers.ini");
    PrizePointer = GetPrivateProfileInt(buffer,"Questprize", 0 , ".\\Pointers.ini");
    HRpointer = GetPrivateProfileInt(buffer,"RewardHRpoints", 0 , ".\\Pointers.ini");
    Mappointer = GetPrivateProfileInt(buffer,"Map", 1 , ".\\Pointers.ini");
    adresslist[0] = Moneypointer;
    adresslist[1] = HRpointer;
    adresslist[2] = PrizePointer;
    adresslist[3] = Mappointer;


    for (i=0; i<=noqd; i++)
    {
        printf("\n<Adress>\n");
        aus.seekp(0x0);
        aus.seekp(adresslist[i], ios::beg);
        printf("\n<Write>");
        aus.write(reinterpret_cast<char*>(&questData[i]), sizeof(questData[i])); 
        Beep(1000,100); 
    }



    i = 0;
}
aus.close();

}

1 个答案:

答案 0 :(得分:0)

您的for循环错误。

for (i=0; i<=noqd; i++)

这是0,1,2,3,4。但是你的数组只从0到3。