したらばTOP ■掲示板に戻る■ 全部 1-100 最新50 | メール | |

管理人の独り言(プログラミング関連)

1323774さん:2009/01/26(月) 19:54:24 ID:fHt2HuPQ0
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <climits>

#include <windows.h>

int main(int argc, char **argv){
FILE *in = std::fopen("input.sar", "rb");
if(!in) return -1;

try{
unsigned char checksum = 0;

for(;;){
int c = std::fgetc(in);
if(c == EOF){
throw std::exception("fgetcがEOFを返した");
}
if(std::isdigit(c)){
//atol的な何か。
unsigned int size = 0;
do{
if(UINT_MAX / 10 - (c - '0') < size){
throw std::exception("オーバーフロー");
}
size = size * 10 + (c - '0');
c = std::fgetc(in);
if(c == EOF){
throw std::exception("fgetcがEOFを返した");
}
}while(std::isdigit(c));

checksum += size;

if(c != 0x0A){
throw std::exception("sar形式のファイルでない");
}

std::fpos_t fpos;
if(std::fgetpos(in, &fpos) != 0){
throw std::exception("fgetpos() Error");
}
fpos += size;
if(std::fsetpos(in, &fpos) != 0){
throw std::exception("fsetpos() Error");
}

c = std::fgetc(in);

if(c != 0x0A){
throw std::exception("sar形式のファイルでない");
}
}else{
if(c == 'C'){
char cs[8];
std::fread(cs, 1, 8, in);
if(std::memcmp(cs, "heckSum=", 8) == 0){
unsigned char f_cs = 0;
for(;;){
c = std::fgetc(in);
if(c == EOF){
throw std::exception("fgetsがEOFを返した");
}
if(!std::isdigit(c)){
break;
}
if(UCHAR_MAX / 10 - (c - '0') < f_cs){
throw std::exception("オーバーフロー");
}
f_cs = f_cs * 10 + (c - '0');
}
if(c != 0x0A){
throw std::exception("sar形式のファイルでない");
}

//最後に実行される

if(checksum == f_cs){
std::cout << "OK!" << std::endl;
}else{
std::cout << "NG!" << std::endl;
}

break;
}else{
throw std::exception("sar形式のファイルでない");
}
}else{
throw std::exception("sar形式のファイルでない");
}
}
}
}catch(std::exception &e){
std::cerr << e.what() << std::endl;
std::fclose(in);
return -2;
}
}


新着レスの表示


名前: E-mail(省略可)

※書き込む際の注意事項はこちら

※画像アップローダーはこちら

(画像を表示できるのは「画像リンクのサムネイル表示」がオンの掲示板に限ります)

掲示板管理者へ連絡 無料レンタル掲示板