Skip to main content
GameDev.net gamedev.net
🔒 Locked

no vfscanf in visual studio?

Started by supagu Apr 2, 2005 at 7:18 PM 7 replies 4.7k views
Original Post
supagu
supagu
seems there is no 'vfscanf' function in visual studio. vfprintf works fine how ever. any one run in to this problem? this is how i wanna use it:

#include <stdio.h>
#include <stdarg.h>
#include <varargs.h>


unsigned int File::ReadString(const char* text, ...)
{
 	if( text == 0 )
    	return 0;

	va_list ap;
  	va_start(ap, text);
	vfscanf(file, text, ap);
  	va_end(ap);

	return 0;
}
Drew_Benton
Drew_Benton
Try _vfscanf and see if that works. I think I had to do that to get snprintf to work.
supagu
supagu
hrmm no luck there either unfortunately :-/
Drew_Benton
Drew_Benton
I found this tidbit:
Quote:

The vfscanf function is analogous to vfprintf(3) and reads
input from the stream pointer stream using a variable
argument list of pointers (see stdarg(3). The vscanf
function scans a variable argument list from the standard
input and the vsscanf function scans it from a string;
these are analogous to the vprintf and vsprintf functions
respectively.


So try using that function, however, I had to remove #include to get it to compile:
#include <stdio.h>#include <stdarg.h>//#include <varargs.h>int main(){	char* text;	FILE *file; 	if( text == 0 )    	return 0;	va_list ap;  	va_start(ap, text);	vfprintf(file, text, ap);  	va_end(ap);	return 0;}


Quote:
int vfprintf( FILE *stream, const char *format, va_list ap);

See if that gets you the desired results as an alternative.
supagu
supagu
int vfscanf(FILE* file, const char *format, va_list argPtr){	// http://www.codeguru.com/Cpp/Cpp/string/comments.php/c5631/?thread=61724	// Get an upper bound for the # of args	size_t count = 0;	const char* p = format;	while(1)	{		char c = *(p++);		if (c == 0) 			break;				if (c == '%' && (p[0] != '*' && p[0] != '%')) 			++count;	}	// Make a local stack	size_t stackSize = (2+count) * sizeof(void*);	void** newStack = (void**)alloca(stackSize);	// Fill local stack the way fscanf likes it	newStack[0] = (void*)file;	newStack[1] = (void*)format;	memcpy(newStack+2, argPtr, count*sizeof(void*));	// Warp into system sscanf with new stack	int result;	void* savedESP;	_asm	{		mov savedESP, esp;		mov esp, newStack;		call fscanf;		mov esp, savedESP;		mov result, eax;	}	return result;}


here are my modifications, it causes a crash, when fscanf is called. so something aint being set up right.

i'm very rough on my ASM, and setting up stack stuffs but im wondering instead of fudging the stack, why not just push the variables in to the current stack?
Kibble
Kibble
Try changing
_asm{	mov savedESP, esp;	mov esp, newStack;	call fscanf;	mov esp, savedESP;	mov result, eax;}

to
_asm{	mov savedESP, esp;	mov esp, newStack;	call dword ptr [fscanf];	mov esp, savedESP;	mov result, eax;}

You are probably using the standard library as a DLL. In MSVC, in the project options, it is Multithreaded Debug DLL, Multithreaded DLL. If this is the case, the above change will fix it.
supagu
supagu
okay yea now that works, but i get a crash when returning from my 'vfscanf' function.

steeping through the assembly is this:
00414A3B call _RTC_CheckStackVars2 (424340h)

which looks like a call to a debug function. so i assume something is not set up properly in the stack anymore?

if i comment out the call to:
call dword ptr [fscanf];

then it all works fine (no crashing) so i assume fscanf is screwing up the stack?
supagu
supagu
okay, i worked out a proper soltuion :D

incase you wanted to know:
int vfscanf(FILE* file, const char *format, va_list argPtr){	// http://www.codeguru.com/Cpp/Cpp/string/comments.php/c5631/?thread=61724	// Get an upper bound for the # of args	size_t count = 0;	const char* p = format;	while(1)	{		char c = *(p++);		if (c == 0) 			break;				if (c == '%' && (p[0] != '*' && p[0] != '%')) 			++count;	}	if (count <= 0)		return 0;	int result;	// copy stack pointer	_asm	{		mov esi, esp;	}	// push variable parameters pointers on stack	for (int i = count - 1; i >= 0; --i)	{		_asm		{			mov eax, dword ptr;			mov ecx, dword ptr [argPtr];			mov edx, dword ptr [ecx+eax*4];			push edx;		}	}	int stackAdvance = (2 + count) * 4;	_asm	{		// now push on the fixed params		mov eax, dword ptr [format];		push eax;		mov eax, dword ptr [file];		push eax;		// call fscanf, and more the result in to result		call dword ptr [fscanf];		mov result, eax;		// restore stack pointer		mov eax, dword ptr[stackAdvance];		add esp, eax;		//mov esp, esi;	}	return result;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.