Tuesday, November 10, 2009

Write an array to 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(message, sizeof(SmsDBase), 200, 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(message, sizeof(SmsDBase), 200, hFile);
fclose(hFile);
}//end of else
return SUCCESS;
}//end of _readItUp()

No comments: