Debug Assertion Failed

Started by
2 comments, last by SiCrane 15 years ago
Hello, I have an array of unsigned chars, and i want to write the values to a file. With the following code i get this runtime error: Debug assertion failed! ... File: f:\dd\vctools\crt_bld\self_x86\crt\src\fprintf.c Line: 56 Expression: (format!=NULL) ...

#include <iostream>
using namespace std;

class CLogger
{
private:
	FILE *Log;
public :
	CLogger(char* Filename)
	{
	Log = fopen(Filename, "w");
	};
	void CloseLog()
	{
		fclose(Log);
	}
	void WriteLog(char *Text)
	{
		fprintf(Log, Text);
	}

};
CLogger Logger("Log.txt");
 
int main()
{   
	Logger.WriteLog("TEST\n"); //WRITING LIKE THIS WORKS
	unsigned char a[3];
	a[0]=0;
	a[1]=1;
	a[2]=2;
	char *text=(char*)a[0];
	Logger.WriteLog(text);
	Logger.CloseLog();
    return 0;     
}

Anybody knows how to solve it? Thank you. :)
Advertisement
fprintf() is supposed to be used for text data, not binary data. Since the string that you're trying to feed to fprintf() starts with a binary 0, also known as a null, fprintf() thinks that you're trying to feed it an empty string as a format parameter. You can try using a different function that expects binary data like fwrite(), but I would recommend against trying to mix text and binary data in a log file like that.
ok, now it's starting to work, just one more thing now,
when using this:
fwrite(Chr,1,sizeof(Chr),Log);
It only writes the first four characters of the array.

#include <iostream>using namespace std;class CLogger{private:	FILE *Log;public :	CLogger(char* Filename)	{	Log = fopen(Filename, "wb");	};	void CloseLog()	{		fclose(Log);	}	void WriteLogChars(char *Text)	{		fwrite(Text,1,strlen(Text), Log);	}	void WriteLogUChars(unsigned char *Chr)	{		fwrite(Chr,1,sizeof(Chr),Log);// sizeof(Chr) seems to return 4, which is not right	}};CLogger Logger("Log.txt");int main(){   	Logger.WriteLogChars("TEST\n"); //WRITING LIKE THIS WORKS	char a[]="chars\n"; // WORKS TOO	unsigned char b[]="unsigned chars"; // WON'T WORK RIGHT, ONLY WRITES FIRST 4 VALUES	b[0]=0;// IT MUST BE POSSIBLE TO WRITE ANY NUMBER BETWEEN 0 AND 255	Logger.WriteLogChars(a);	Logger.WriteLogUChars(b);	Logger.CloseLog();    return 0;     }


I know it's weird that i try to write text AND values in the same log file :)
But i would like to make it work.

When you use sizeof on a pointer, it tells you the size of the pointer, not the size of the data that the pointer points to. You'll need to tell your function how much data to write. It won't be able to automagically determine it on its own.

This topic is closed to new replies.

Advertisement