Предыдущая тема :: Следующая тема |
Автор |
Сообщение |
McZim
Зарегистрирован: 09.08.2005 Сообщения: 275 Откуда: Omsk
|
Добавлено: Ср Авг 24 2005 13:21 Заголовок сообщения: Контрольная сумма файла |
|
|
Здравствуйте. Помогите пожалуйста посчитать контрольную сумму по байтно.
Код: | {int SecCount=1,index=0;
SecCount+=FDKCount>0;
SecCount+=FTimeMapCount>0;
SecCount+=FTimeProgCount>0;
SecCount+=FPlanCorCount>0;
TInfo info;
TSections *Sections;
Sections = new TSections[SecCount];
if(FDKCount>0)
{
strcpy(Sections[index].type,("DK"));
Sections[index].begin = sizeof(struct TSections)*SecCount+sizeof(struct TInfo);
Sections[index].len = sizeof(struct TDK)*FDKCount;
index++;
}
if(FTimeMapCount>0)
{
strcpy(Sections[index].type,("TimeMap"));
if(index>0)
Sections[index].begin = Sections[index-1].begin + Sections[index-1].len;
else
Sections[index].begin = sizeof(struct TSections)*SecCount;
Sections[index].len = sizeof(struct TTimeMap)*FTimeMapCount;
index++;
}
if(FTimeProgCount>0)
{
strcpy(Sections[index].type,("TimeProg"));
if(index>0)
Sections[index].begin = Sections[index-1].begin + Sections[index-1].len;
else
Sections[index].begin = sizeof(struct TSections)*SecCount;
Sections[index].len = sizeof(struct TTimeProg)*FTimeProgCount;
index++;
}
if(FPlanCorCount>0)
{
strcpy(Sections[index].type,("PlanCor"));
if(index>0)
Sections[index].begin = Sections[index-1].begin + Sections[index-1].len;
else
Sections[index].begin = sizeof(struct TSections)*SecCount;
Sections[index].len = sizeof(struct TPlanCor)*FPlanCorCount;
index++;
}
strcpy(Sections[index].type,("END"));
Sections[index].begin = 0;
Sections[index].len = 0;
if(dlgSaveFile->Execute())
{
String File=dlgSaveFile->FileName+".me";
writefile=File.c_str();
HANDLE wFile;
wFile = CreateFile(writefile,GENERIC_WRITE | GENERIC_READ,FILE_SHARE_WRITE | FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
strcpy(info.strt,TimeToStr(Time()).c_str());
strcpy(info.strd,DateToStr(Date()).c_str());
info.sum=0xFEEEEEEF;//инициализируем 4-байта под сумму и пишем ее вместе со структурой info
WriteFile(wFile,&info,sizeof(struct TInfo),&ByteCount,NULL);
WriteFile(wFile,Sections,sizeof(struct TSections)*SecCount,&ByteCount,NULL);
WriteFile(wFile,FDK,sizeof(struct TDK)*FDKCount,&ByteCount,NULL);
WriteFile(wFile,FTimeMap,sizeof(struct TTimeMap)*FTimeMapCount,&ByteCount,NULL);
WriteFile(wFile,FTimeProg,sizeof(struct TTimeProg)*FTimeProgCount,&ByteCount,NULL);
WriteFile(wFile,FPlanCor,sizeof(struct TPlanCor)*FPlanCorCount,&ByteCount,NULL);
CloseHandle(wFile);
}
delete[] Sections;
HANDLE wFile;
wFile = CreateFile(writefile,GENERIC_WRITE | GENERIC_READ,FILE_SHARE_WRITE | FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
ReadFile(wFile,&info,sizeof(struct TInfo),&ByteCount,NULL);
for(...)
{
info.sum+=;
}
} |
затем открываем только что записанный нами файл считаем его по байтно, перемещаемся в позицию про инициализированную под сумму и записываем сумму! НО ВОТ НЕ ЗНАЮ Я КАК ЭТО СДЕЛАТЬ, ЕСЛИ ЕСТЬ ПУТЬ ПРОЩЕ ПОДСКАЖИТЕ! СПАСИБО!!! |
|
Вернуться к началу |
|
|
McZim
Зарегистрирован: 09.08.2005 Сообщения: 275 Откуда: Omsk
|
Добавлено: Чт Авг 25 2005 06:58 Заголовок сообщения: |
|
|
Фсе вобщем я разобрался
Код: | if(dlgSaveFile->Execute())
{
String File=dlgSaveFile->FileName+".me";
writefile=File.c_str();
HANDLE wFile;
wFile = CreateFile(writefile,GENERIC_WRITE | GENERIC_READ,FILE_SHARE_WRITE | FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
strcpy(info.strt,TimeToStr(Time()).c_str());
strcpy(info.strd,DateToStr(Date()).c_str());
info.sum=0;
WriteFile(wFile,&info,sizeof(struct TInfo),&ByteCount,NULL);
WriteFile(wFile,Sections,sizeof(struct TSections)*SecCount,&ByteCount,NULL);
WriteFile(wFile,FDK,sizeof(struct TDK)*FDKCount,&ByteCount,NULL);
WriteFile(wFile,FTimeMap,sizeof(struct TTimeMap)*FTimeMapCount,&ByteCount,NULL);
WriteFile(wFile,FTimeProg,sizeof(struct TTimeProg)*FTimeProgCount,&ByteCount,NULL);
WriteFile(wFile,FPlanCor,sizeof(struct TPlanCor)*FPlanCorCount,&ByteCount,NULL);
unsigned int s=GetFileSize(wFile,NULL);//берем размер файла
info.sum=0xFEEEEEEF;//инициализируем
SetFilePointer(wFile,sizeof(struct TInfo),NULL,FILE_BEGIN);//говорим откуда будем считать
for(unsigned int k=0;k<s;k++)
{
unsigned char L;
ReadFile(wFile,&L,1,&ByteCount,NULL);
info.sum+=L;//по байтно складываем
}
SetFilePointer(wFile,0,NULL,FILE_BEGIN);//пишем в файл вот с этого места
WriteFile(wFile,&info,sizeof(struct TInfo),&ByteCount,NULL);
CloseHandle(wFile);
}
delete[] Sections; |
|
|
Вернуться к началу |
|
|
|