Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (SBGuy), 652 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Problem with file_str_read... #422702
05/15/13 09:48
05/15/13 09:48
Joined: May 2013
Posts: 3
N
NNH Offline OP
Guest
NNH  Offline OP
Guest
N

Joined: May 2013
Posts: 3
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

Last edited by NNH; 05/15/13 09:57.
Re: Problem with file_str_read... [Re: NNH] #422703
05/15/13 10:09
05/15/13 10:09
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline
Expert
oliver2s  Offline
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Shouldn't ddat and did a var instead of integer?

Re: Problem with file_str_read... [Re: oliver2s] #422707
05/15/13 10:37
05/15/13 10:37
Joined: May 2013
Posts: 23
Somewhere
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

Joined: May 2013
Posts: 23
Somewhere
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.


His words are wise, his face is beard.
Re: Problem with file_str_read... [Re: oliver2s] #422708
05/15/13 10:38
05/15/13 10:38
Joined: May 2013
Posts: 3
N
NNH Offline OP
Guest
NNH  Offline OP
Guest
N

Joined: May 2013
Posts: 3
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);
}


Last edited by NNH; 05/15/13 10:52.
Re: Problem with file_str_read... [Re: NNH] #422709
05/15/13 11:01
05/15/13 11:01
Joined: May 2013
Posts: 23
Somewhere
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

Joined: May 2013
Posts: 23
Somewhere
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.


His words are wise, his face is beard.
Re: Problem with file_str_read... [Re: Wiseguy] #422710
05/15/13 11:10
05/15/13 11:10
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
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.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Problem with file_str_read... [Re: Superku] #422713
05/15/13 11:22
05/15/13 11:22
Joined: May 2013
Posts: 3
N
NNH Offline OP
Guest
NNH  Offline OP
Guest
N

Joined: May 2013
Posts: 3
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?

Last edited by NNH; 05/15/13 11:30.
Re: Problem with file_str_read... [Re: NNH] #422715
05/15/13 11:40
05/15/13 11:40
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline
Expert
oliver2s  Offline
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
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).


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1