Memory cannot be read?
Hi there it's me again.
this is with regards to the thread Debug Assertion Failed!
well i've finally figured out what causing the problem,i failed to add a enviroment variable w/c value is the location of the cfg file.
i've pre-run the program and i it works fine.so i decided to continue to code.
now the problem is that when i run the program again i've got this message
( http://i20.photobucket.com/albums/b236/geebee0901/popup.jpg)
can any one help figure out what's wrong. i'm pretty much stuck here
thanks in advance,
dlare9
here are my codes...
RMServer.c
// RMServer side
#include "RMServer.h"
char msg[50];
char logfile[100]="";
int error;
struct RMConfig cfg;
extern int WriteToLog(char* str);
extern void displayVersion();
//all printf() are used for debugging
int main(){
WSADATA wsda;
WSAStartup(0x0101,&wsda);
// Start Logger
printf("0 0");
logStart();
// end Logger
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
//printf("Socket error...");
WriteToLog("Socket error...");
return 0;
}
server.sin_addr.s_addr=INADDR_ANY; /*INADDR_ANY will simply put your IP */
server.sin_port=htons(PORT); /*htons means host to network short */
server.sin_family=AF_INET;
if(bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1)
{
//printf("Cannot bind...");
WriteToLog("Error! Cannot bind...");
return 0;
}
if(listen(sockfd,BACKLOG))/*Listening */
{
//printf("Error Listening...");
WriteToLog("Error! Listening...");
return 0;
}
while (1)
{
int size=sizeof(struct sockaddr_in);
if((sockfd2=accept(sockfd,(struct sockaddr*)&client,&size))==-1)
{
//printf("Accept Error...");
WriteToLog("Accept Error!...");
return 0;
}
if((n_bytes=recv(sockfd2,msg ,50,0))==-1){
//printf("Error Recv...");
WriteToLog("Error Recv!...");
return 0;
}
msg[n_bytes]='\0';
printf("Client's Message:%s",msg);
WriteToLog("Client's Message : ");
WriteToLog(msg);
}
WSACleanup();
closesocket(sockfd); /*used to close the socket */
closesocket(sockfd2);
return 0;
}// end main
// log init
int logStart()
{
int error=0;
char logfile[100]="";
char *rmConfig;
printf(" logStart() ");
if((rmConfig = getenv(RM_SYS_VAR)) != NULL)
{
sprintf(CONFIG_FILE,"%s",rmConfig);
}
else
{
printf("no cfg file found!");
return 1;
}
//strcpy(CONFIG_FILE ,"C://RISK//RMResource//RM.cfg"); //hard coded the location of config file
printf("0 ");
cfg = initConfig();
printf("1 ");
cfg = getLogger(LOG_CONFIG,cfg);
printf("2 ");
strcpy(logfile,cfg.log.logDirectory);
printf("3 ");
strcat(logfile, cfg.log.logFile);
printf("4 ");
strcpy(LOGFILE,logfile);
//printf("logfile %s ",logfile);
error = WriteToLog("Monitoring started.");
printf("gerald ");
if (!error)
{
printf("deeh ");
displayVersion();
sprintf(logfile,"RM CONFIG FILE = %s",CONFIG_FILE);
WriteToLog(logfile);
}
return(error);
}
RMUtil.c
#include "RMUtil.h"
char errorNum[20]="";
char tempVal[100]="";
int WriteToLog(char* str)
{
time_t t;
FILE* log;
char today[50]="";
t = time(NULL);
printf("b ");
sprintf(today,"%s",ctime(&t));
printf("c ");
today[strlen(today)-1] = '\0';
printf("d ");
if(LOGFILE == '\0')
{
printf("e ");
log = fopen(LOGFILE, "a+");
printf("f ");
}
if (log == NULL)
{
printf("g ");
return -1;
}
printf("h ");
fprintf(log, "%s %s\n", today, str); // i think this is causing the problem but don't know wat to do
printf("i ");
fclose(log);
printf("j ");
return 0;
}
int writeToFile(char *file, char* str)
{
char logger[100]="";
time_t t;
FILE* log;
char today[50]="";
t = time(NULL);
sprintf(today,"%s",ctime(&t));
today[strlen(today)-1] = '\0';
log = fopen(file, "w");
if (log == NULL){
sprintf(logger, "FAILED to open file: %s. Make sure that the directory path exists.", file);
WriteToLog(logger);
return 1;
}
fprintf(log, "%s %s\n", today, str);
fclose(log);
return 0;
}
char* removeNewline(char line[])
{
if (line[strlen(line)-1] == '\n')
{
line[strlen(line)-1] = '\0';
}
return line;
}
char* getValue(char *line)
{
char value[130]="";
char str[130]="";
int i=0, k=0;
strcpy(value, line);
if(value[0] == ' ')
{
while(value[i] == ' '){
i++;
}
while(value[i] != '\0' && value[i] != '\n'){
str[k]= value[i];
k++;i++;
}
str[k]='\0';
strcpy(value,str);
}
if(value[0] == '\"'){
i=1;k=0;
while(value[i] != '\0' && value[i] != '\"' && value[i] != '\n'){
str[k] = value[i];
i++; k++;
}
str[k]='\0';
strcpy(value,str);
}
return removeNewline(value);
}
struct RMConfig initConfig()
{
struct RMConfig cfg;
struct tm time;
time.tm_hour =0;
time.tm_mday =1;
time.tm_min =0;
time.tm_mon = 1;
time.tm_year = 1900;
strcpy(cfg.log.logFile,"");
strcpy(cfg.log.logDirectory,"");
strcpy(cfg.log.logArchiveDirectory,"");
strcpy(cfg.log.nextLogClearStr,"");
strcpy(cfg.version.build_date_Str,"");
strcpy(cfg.version.version,"");
strcpy(cfg.version.system_name,"");
return cfg;
}
struct RMConfig getLogger(int configType, struct RMConfig cfg)
{
FILE *configFile;
char msg[30];
char val[200];
char ch;
char cfgTitle[50]="";
if ((configFile = fopen( CONFIG_FILE, "r" )) == NULL)
{
return cfg;
}
if (configType == LOG_CONFIG){
strcpy(cfgTitle,LOG_CONFIG_TITLE);
}
while(!feof(configFile)){
fscanf(configFile,"%s", msg);
if (strcmp(cfgTitle,msg) == 0){
if (configType == LOG_CONFIG) {
while(!feof(configFile) && (ch = fgetc(configFile)) != EOF){
if (ch == '#')
return cfg;
if (ch != '\n'){
fseek(configFile,-1, SEEK_CUR);
fscanf(configFile,"%s", msg);
fgets(val,sizeof(val),configFile);
strcpy(val,getValue(val));
if(!strcmp(msg,LOGFILE_LABEL))
{
strcpy(cfg.log.logFile,val);
}
if(!strcmp(msg,LOG_DIRECTORY_LABEL))
{
strcpy(cfg.log.logDirectory,val);
}
if(!strcmp(msg,LOG_ARCHIVE_DIRECTORY_LABEL))
{
strcpy(cfg.log.logArchiveDirectory,val);
}
}
}
}
}
}
fclose(configFile);
return cfg;
}
char* getPropertyValue(char *filename, char *property)
{
char log[100]="";
char val[100]="";
char key[100]="";
//char ch;
FILE *propertyFile;
if ((propertyFile = fopen(filename, "r" )) == NULL)
{
sprintf(log, "FAILED to open file: %s. Make sure that the file and the directory path exists.", filename);
WriteToLog(log);
}
else
{
while(!feof(propertyFile)){
fscanf(propertyFile,"%s", key);
if (strcmp(key,property) == 0){
fgets(val,sizeof(val),propertyFile);
strcpy(val,getValue(val));
}
}
fclose(propertyFile);
}
if(strcmp(val,"")==0)
{
sprintf(log,"WARNING: Property name '%s' is not found in %s", property,filename);
WriteToLog(log);
}
strcpy(tempVal,"");
strcpy(tempVal,val);
return tempVal;
}
struct RMConfig getConfig(int configType,struct RMConfig cfg)
{
char log[100]="";
char cfgTitle[50]="";
// sprintf(log,"Getting configuration type: %d",configType );
// WriteToLog(log);
if (configType == LOG_CONFIG){
strcpy(cfg.log.logFile,getPropertyValue(CONFIG_FILE, LOGFILE_LABEL));
strcpy(cfg.log.logDirectory,getPropertyValue(CONFIG_FILE, LOG_DIRECTORY_LABEL));
strcpy(cfg.log.logArchiveDirectory,getPropertyValue(CONFIG_FILE, LOG_ARCHIVE_DIRECTORY_LABEL));
}
if (configType == VERSION_CONFIG){
strcpy(cfg.version.system_name,getPropertyValue(CONFIG_FILE, SYSTEM_NAME_LABEL));
}
return cfg;
}
void displayVersion()
{
struct RMConfig cfg;
char log[100]="";
cfg = initConfig();
cfg = getConfig(VERSION_CONFIG,cfg);
sprintf(log,"%s",cfg.version.system_name);
WriteToLog(log);
sprintf(log,"Version: %s",VERSION);
WriteToLog(log);
sprintf(log,"Build date: %s",BUILD_DATE);
WriteToLog(log);
}
RMServer.h
#include <stdio.h>
#include <winsock.h>
#include "RMUtil.h"
#define PORT 1200
#define BACKLOG 4
struct sockaddr_in server;
struct sockaddr_in client;
int sockfd,sockfd2,n_bytes;
int main();
int logStart();
RMUtil.h
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include "RMConstants.h"
char LOGFILE[200];
char CONFIG_FILE[100];
struct logInfo
{
struct tm nextLogClear;
char logFile[ 30 ];
char logDirectory[ 200 ];
char logArchiveDirectory[ 200 ];
char nextLogClearStr[ 16 ];
int daysBeforeArchiveLog;
};
struct versionInfo{
struct tm build_date;
char build_date_Str[20];
char version[8];
char system_name[50];
};
struct RMConfig{
struct logInfo log;
struct versionInfo version;
};
int WriteToLog(char* str);
int writeToFile(char *file, char* str);
char* removeNewline(char line[]);
char* getValue(char *line);
struct RMConfig initConfig();
struct RMConfig getLogger(int configType, struct RMConfig cfg);
char* getPropertyValue(char *filename, char *property);
struct RMConfig getConfig(int configType,struct RMConfig cfg);
void displayVersion();
RMConstants.h
#include<time.h>
#define BUILD_DATE "2005-11-11"
#define VERSION "beta 0.0.1"
#define LOG_CONFIG_TITLE "#log"
#define LOGFILE_LABEL "log_file"
#define LOG_DIRECTORY_LABEL "log_directory"
#define LOG_ARCHIVE_DIRECTORY_LABEL "log_archive_directory"
#define RM_SYS_VAR "PNL"
#define VERSION_CONFIG_TITLE "#version"
#define SYSTEM_NAME_LABEL "system_name"
#define LOG_CONFIG 259
#define VERSION_CONFIG 260
and btw.. you have to create an enviroment variable called " RM" having the value of the cfg file (i.e. "C:\RISK\RMResource\RM.cfg"). And also a you have to create a RM.log file located where the RM.cfg is placed.
and the RM.cfg file contains the ff:
#version
system_name RISK MANAGEMENT
#log
log_file RM.log
log_directory C:\\RISK\RMResource\
log_archive_directory C:\\RISK\RMResource\log archive\
|