GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2005, 06:59
dlare9 dlare9 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
dlare9 is on a distinguished road
Question

Debug Assertion Failed!


Hi!
Tthis is my first time posting.
I've created a program (win32 console app) w/c is basically a server that will get a message though a client (prog. in java). I've tested the program and it works just fine. So i decided to add some functions, like a WriteToLog function, I compiled and build the program without a problem.
Now the proble is when I try to run the program I get this popup message:

=================================
Debug Assertion Failed!

Program: C:\RISK\PROJ\RM\Debug\RM.exe
File: fopen.c
Line: 54

Expression: *file != _T('\0')
==================================

I'm new at C and I can't figure out what this mean.
Did I make a mistake in coding or in setting up the project?
Any help will be much appreciated.

thanks, dlareg
  #2  
Old 11-Nov-2005, 07:07
dlare9 dlare9 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
dlare9 is on a distinguished road

Re: Debug Assertion Failed!


btw here are my codes


RMServer.c
CPP / C++ / C Code:
// RMServer side
#include "RMServer.h"

char msg[50]; 
char logfile[100]="";
int error;
struct RMConfig cfg;
extern int WriteToLog(char* str);
extern void displayVersion();

int main(){ 

	WSADATA wsda; 
	WSAStartup(0x0101,&wsda); 

	// Start Logger
	logStart();
	// end Logger


	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{ 
	printf("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..."); 
	return 0; 
	} 

	if(listen(sockfd,BACKLOG))/*Listening */
	{ 
	printf("Error Listening..."); 
	return 0; 
	} 

	while (1)
	{ 

		int size=sizeof(struct sockaddr_in); 

		if((sockfd2=accept(sockfd,(struct sockaddr*)&client,&size))==-1)
		{ 
		printf("Accept Error..."); 
		return 0; 
		} 

		if((n_bytes=recv(sockfd2,msg ,50,0))==-1){ 
		printf("Error Recv..."); 
		return 0; 
		} 

		msg[n_bytes]='\0'; 

		printf("Client's Message:%s",msg); 

	} 

	WSACleanup(); 
	closesocket(sockfd); /*used to close the socket */ 
	closesocket(sockfd2); 

	return 0; 
}// end of main 



// log init
int logStart() 
{ 
    int error=0;
	char logfile[100]="";
//	char *rmConfig;
	
/*	
	if((rmConfig = getenv(RM_SYS_VAR)) != NULL)
	{
		sprintf(PNL_CONFIG_FILE,"%s",rmConfig);	
	}
	else 
	{
		return 1;
	}
*/

	cfg = initConfig();
	//cfg = getConfig(LOG_CONFIG,cfg);
	cfg = getLogger(LOG_CONFIG,cfg);
	strcpy(logfile,cfg.log.logDirectory);
	strcat(logfile, cfg.log.logFile);
	strcpy(LOGFILE,logfile);
	error = WriteToLog("Monitoring started.");
	if (!error)
	{
		displayVersion();
		sprintf(logfile,"RM CONFIG FILE = %s",PNL_CONFIG_FILE);
		WriteToLog(logfile);
	}
    return(error); 
} 


RMServer.c
CPP / C++ / C Code:
#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 logStart();


RMUtil.c
CPP / C++ / C Code:
#include "RMUtil.h"

char errorNum[20]="";
char tempVal[100]="";

int WriteToLog(char* str)
{
	
	time_t t;
	FILE* log;
    char today[50]="";
	t = time(NULL);

	sprintf(today,"%s",ctime(&t));
	today[strlen(today)-1] = '\0';

	log = fopen(LOGFILE, "a+");
	if (log == NULL)
		return -1;
	fprintf(log, "%s %s\n", today, str);
	fclose(log);
	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( PNL_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(PNL_CONFIG_FILE, LOGFILE_LABEL));
		strcpy(cfg.log.logDirectory,getPropertyValue(PNL_CONFIG_FILE, LOG_DIRECTORY_LABEL));
		strcpy(cfg.log.logArchiveDirectory,getPropertyValue(PNL_CONFIG_FILE, LOG_ARCHIVE_DIRECTORY_LABEL));
	}
	if (configType == VERSION_CONFIG){
		strcpy(cfg.version.system_name,getPropertyValue(PNL_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);
}




RMUtil.h
CPP / C++ / C Code:
#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 PNL_CONFIG_FILE[100] = "C://RISK/RMResource/PNL.cfg";

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 resetInfo reset;
	struct logInfo log;
//	struct dbConfig db;
//	struct mqConfig mq_write;
//	struct mqConfig mq_read;
	struct versionInfo version;
//	struct paramInfo param;
//	struct debug debugger;
//	struct discard discard;
};


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
CPP / C++ / C Code:
#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 "RM"

#define VERSION_CONFIG_TITLE "#version"
#define SYSTEM_NAME_LABEL "system_name"

#define LOG_CONFIG 259
#define VERSION_CONFIG 260

#define PNL_CONFIG_FILE "C://RISK/RMResource/PNL.cfg"


and finally the contents of the cfg file (RM.log)

#version
system_name RISK MANAGEMENT

#log
log_file RM.log
log_directory C:\\RISK\RMResource\
log_archive_directory C:\\RISK\RMResource\log archive\
  #3  
Old 11-Nov-2005, 07:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Debug Assertion Failed!


Quote:
Originally Posted by dlare9
Hi!
Now the proble is when I try to run the program I get this popup message:

=================================
Debug Assertion Failed!

Program: C:\RISK\PROJ\RM\Debug\RM.exe
File: fopen.c
Line: 54

Expression: *file != _T('\0')
==================================

Since the message has something to do with "fopen", look at places where you use fopen:

CPP / C++ / C Code:
	log = fopen(LOGFILE, "a+");
.
.
.
	log = fopen(file, "w");
.
.
.

What is "LOGFILE" here? What is "file"? They are supposed to be names of the files that you are trying to open.

Since the debug junk is hiding the truth from you, make the program tell you what it's working with:

CPP / C++ / C Code:
  printf("My Very Own Debug Statement #1: LOGFILE: <%s>\n", LOGFILE);
  log = fopen(LOGFILE, "a+");
.
.
.
  printf("My Very Own Debug Statement #2: file: <%s>\n", file);
  log = fopen(file, "w");
.
.
 /* etc. */


The debug stuff is supposed to help you, but if it just gives you the error message you posted and didn't even tell you where in your source code it was working, I think that's pretty pitiful.

However, you can always put your own debugging statements throughout your code to try to pinpoint the spot of the area and, maybe, overcome the shortcomings of whatever system you are working with.


Regards,

Dave
Last edited by davekw7x : 11-Nov-2005 at 08:13.
  #4  
Old 13-Nov-2005, 23:18
dlare9 dlare9 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
dlare9 is on a distinguished road

Re: Debug Assertion Failed!


Thanks for the reply dave.
sorry I did not response to your reply quickly.
LOGFILE is the RM.log.
Through countless printf I was able to find out what the problem is. The problem is that the program can't read the RM.cfg file, and since it was not read the LOGFILE has no value and thus the error was displayed.
I will try to find a workaround on this problem and will post again if I encounter some stoping block.

again thanks for the response,
dlare9
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect Removable USB Device Automatik C Programming Language 17 24-Sep-2009 05:19
Debug assertion failed BrucePataki MS Visual C++ / MFC Forum 2 02-Nov-2008 06:01
MFC Assertion Failed patti_ MS Visual C++ / MFC Forum 3 21-Jul-2005 00:50
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Non-local heaps and assertion errors Marius MS Visual C++ / MFC Forum 1 03-Sep-2004 05:45

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:18.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.