C Functions

BlitzMax Forums/BlitzMax Programming/C Functions

kmac(Posted 2005) [#1]
I'm finally moving from being an observer to using my copy of BlitzMax. As a consequence this inquiry has a bit more substance.

Given the following text file (test2.txt),

blah1 8 12
blah2 3 7


I would like to read it with a set of C functions (func.c)

#include<stdio.h>
#include<stdlib.h>


void bprintf(int a, char *name)
{
	printf("%d, %s", a, name);
}

int bsscanf(FILE *fp1, char *name, int *a, int *b)
{
	char buf[120];
	int i = 0;
	while(fgets(buf, sizeof(buf), fp1) !=NULL)
	{
	if (sscanf(buf, "%s %d %d", &name[i], &a[i], &b[i]) !=3)
	{
	}
	i++;
	}

	return i;
}


void bfprintf(FILE *fp1, int index, int a, char *name)
{
	fprintf(fp1, "%d, %s", a, name);
}





However, it doesn't appear to work with the following (incorrect) BlitzMax code (read.bmx),


Import "c_func.c"
Import "include/*.h"


Type Data
	Field Name$
	Field a%
	Field b%
End Type


Extern "c"
	Function bprintf(num, str$z)	
	Function bsscanf(fp1, str$z, a, b)
	Function fopen(fname$z, fmode$z)="fopen"
	Function fclose(c_stream)="fclose"
End Extern

	
	
Global DataArray:Data[]
Local in:Data = New Data 


fp1 = fopen("test2.txt", "r")
bsscanf(fp1, in.Name, in.a, in.b)
fclose(fp1)

Print "sscanf: " + DataArray[1].Name$ + " " + DataArray[1].a + " " + DataArray[1].b

bprintf(DataArray[1].a, DataArray[1].Name$)




Any corrections would be much appreciated. On the surface and some other experimentation it appears that I will in time be able to make use of a mix of BlitzMax and C...


N(Posted 2005) [#2]
I rewrote your code:
Strict '' Use Strict, or you'll regret it.

Import "c_func.c"
'' Don't import header files -- that doesn't work.  Nor do wildcards, if I recall correctly.

'' Putting "C" isn't neccessary as far as I've seen
Extern
'' Ptr = Your friend
	Function bprintf( num, str$z )	
	Function bsscanf( fp1:Int Ptr, str$z, a:Int Ptr, b:Intr Ptr )
	Function fopen:Int Ptr( fname$z, fmode$z )
	Function fclose( c_stream:Int Ptr )
End Extern

Type Data
	Field Name$
	Field a%
	Field b%
End Type

'' Why is that array even there?
Global DataArray:Data[]

'' Shouldn't this have been put into the array?
Local in:Data = New Data 

'' Local is your friend too.
Local fp1:Int Ptr = fopen( "test2.txt", "r" )

'' Varptr is your friend.
bsscanf( fp1, in.Name, Varptr in.a, Varptr in.b )

'' Nullify pointers so you won't accidentally use them again.
fclose( fp1 )
fp1 = Null

'' I don't know why you're printing from the array, when DataArray[1] should be Null.
Print "sscanf: " + in.Name$ + " " + in.a + " " + in.b

bprintf( in.a, in.Name$ )


I'm also compelled to mention that doing what you're doing is really, really not worth the trouble.

Edit: And before you ask, no, I didn't make sure it works, because what you're doing here is really not that good of an idea.


kmac(Posted 2005) [#3]
Noel. First thank you very much for the new version. I'll have a look shortly.

There are two reasons for attempting the above.

1. It provides the opportunity to learn more about BlitzMax and the integration with C. Presumably I may derive benefit when it comes time to make use of existing C libraries coupled with my own functions. -- Lastnight was the first time I actually sat down with BM.

2. Thus far I haven't found an equivalent to fprintf, fscanf, or sscanf. At the moment these functions appear to be more flexible for reading and writing tables. Even printf better for many situations. If there really is a better/equivalent BM solution available I'd gladly switch. In the code that I wrote lastnight I'm using previously posted BM functions that at the moment appear too inflexible (which may be a by-product of my novice status). Consequently, I'm exploring.

Edit: hmmm...doesn't appear to work but will investigate later.

Edit2: For the time being I've settled on separating fields with a slash and reading with a BM function.