Friday, October 16, 2009

Reading and Writing a pointer of continously memory to the file in C

int _backItUp(){
FILE * hFile;
//************************
char *_fileName;
_fileName = getenv("USER");
char buffer[200];
struct timeval tv;
time_t curtime;
gettimeofday(&tv, NULL);
curtime=tv.tv_sec;
strftime(buffer,30,"%Y%m%d_%H%M",localtime(&curtime));
//"%m-%d-%Y %T"
strcat(_fileName,"_");
strcat(_fileName,buffer);
strcat(_fileName,".bak");
//*******************************
hFile = fopen( _fileName, "w");
if (hFile == NULL){
// Error, file not found
printf("*** Failed to open the file ***\n");
return FAILED;
}//end of if
else{
// Process & close file
int nWritten = fwrite(allocatedMem, sizeof(unsigned char), (200*sizeof(SmsDB))+((75)*sizeof(unsigned char))+((200*3)*sizeof(dBlock)), hFile);
printf("*** Taking backup ***\n");
fclose(hFile);
}//end of else
return SUCCESS;
}//end of backItUp()

int _readItUp(){
FILE * hFile;
hFile = fopen( ___fileName, "r");
if (hFile == NULL){
// Error, file not found
printf("*** Failed to open the file ***\n");
return FAILED;
}//end of if
else{
// Process & close file
int nRead = fread(allocatedMem, sizeof(unsigned char), (200*sizeof(SmsDB))+((75)*sizeof(unsigned char))+((200*3)*sizeof(dBlock)), hFile);
fclose(hFile);
}//end of else
return SUCCESS;
}//end of _readItUp()

No comments: