Problem with file_str_read...

Posted By: NNH

Problem with file_str_read... - 05/15/13 09:48

When I'm trying to run this code, it gave me this error: "Crash in import". But if I comment out the following lines:

Code:
while(i < c && ddat != -1) 
	{
		
		// file_str_read(ddat,read);
		box[i].name = read;
		
		// box[i].id = file_var_read(did);
		
		i++;
		
		wait(1);
	}



it runs fine. What am I doing wrong?
Here's my code:

Code:
#include <acknex.h>

#define c 3

typedef struct chembox {
	STRING 	*name;
	int 		id;
	int 		state;
} chembox;

void import();

//////////////////////////////////////////////////////////////
chembox box[c];

void import() 
{
	int i = 0, ddat, did;
	STRING *read = "  ";
	
	ddat = file_open_read("ddat.txt");
	did = file_open_read("did.txt");
	
	while(i < c && ddat != -1) 
	{
		
		file_str_read(ddat,read);
		box[i].name = read;
		
		box[i].id = file_var_read(did);
		
		i++;
		
		wait(1);
	}
	
	if(!ddat) file_close(ddat);
	if(!did) file_close(did);
}

void main(void) 
{
	level_load("");
	
	import();
	
	wait(1);
}



And these are two files contain the data:

ddat.txt
Na,Na,Be,

did.txt
0 1 5
Posted By: oliver2s

Re: Problem with file_str_read... - 05/15/13 10:09

Shouldn't ddat and did a var instead of integer?
Posted By: Wiseguy

Re: Problem with file_str_read... - 05/15/13 10:37

Originally Posted By: oliver2s
Shouldn't ddat and did a var instead of integer?

It doesn't matter, the compiler will promote the var to an integer implicitly.

The problem is this line: STRING *read = " ";
The STRING is actually a const char *, not a STRING *. Use str_create() to create a temporary string and str_remove() to clean it up afterwards.

Also; You don't need a wait(1) in your loop.
Posted By: NNH

Re: Problem with file_str_read... - 05/15/13 10:38

Thanks for your fast reply!

I changed to var and it worked. Didn't notice that. wink But, like Wiseguy said, I just don't understand why var worked but not for int, because afaik, var is just an extended type of int.

Here's my code after I had made a few changes.
Code:
void import() {
	var i = 0, ddat, did;
	
	while(i < c) {
		
		STRING *read = str_create("#8");
		
		file_str_read(ddat,read);
		box[i].name = _chr(read);
		
		box[i].id = file_var_read(did);
		str_remove(read);
		
		i++;
	}
	
	if(!ddat) file_close(ddat);
	if(!did) file_close(did);
}

Posted By: Wiseguy

Re: Problem with file_str_read... - 05/15/13 11:01

Your code is still prone to errors! This line right here, accesses the char pointer and assigns it to name:
Code:
box[i].name = _chr(read);



Once you remove the STRING via str_remove(), the pointer becomes invalid because it's handled by the owning STRING.

Quote:
I just don't understand why var worked but not for int, because afaik, var is just an extended type of int.

var is a fixed pointer number, while int is a boring integer. I was pretty sure that the file_open() methods would return integer handles, but if the automatic type promotion didn't work, then that's apparently not the case.
Posted By: Superku

Re: Problem with file_str_read... - 05/15/13 11:10

Quote:
I was pretty sure that the file_open() methods would return integer handles

No, it returns regular var numbers (such as 12543.731), as described in the manual. Never assume something about a function that you don't really know, esp. when the manual tells you different.
Posted By: NNH

Re: Problem with file_str_read... - 05/15/13 11:22

I don't know if it is right or not, but when i put this line in my code, it worked:
Code:
box[i].name = _chr(read);



I was trying to copy the string from the text file to box[i].name, but str_cpy doesn't work for me. It gave me the error: "Invalid arguments".
Code:
str_cpy(box[i].name,read);



So how can I copy it properly?
Posted By: oliver2s

Re: Problem with file_str_read... - 05/15/13 11:40

It seems the string isn't created. Try this:

Code:
box[i].name = str_create(read);



If you use box[i].name a second time, then str_cpy will work and you don't need to create a new string (or remove the old string via str_remove and then create a new one).
© 2024 lite-C Forums