1 registered members (SBGuy),
652
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Problem with file_str_read...
#422702
05/15/13 09:48
05/15/13 09:48
|
Joined: May 2013
Posts: 3
NNH
OP
Guest
|
OP
Guest
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:
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:
#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: oliver2s]
#422707
05/15/13 10:37
05/15/13 10:37
|
Joined: May 2013
Posts: 23 Somewhere
Wiseguy
Newbie
|
Newbie
Joined: May 2013
Posts: 23
Somewhere
|
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
NNH
OP
Guest
|
OP
Guest
Joined: May 2013
Posts: 3
|
Thanks for your fast reply! I changed to var and it worked. Didn't notice that. 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.
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
Wiseguy
Newbie
|
Newbie
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:
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. 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
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
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
NNH
OP
Guest
|
OP
Guest
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:
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".
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
Expert
|
Expert
Joined: Aug 2002
Posts: 3,258
Mainz
|
It seems the string isn't created. Try this:
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).
|
|
|
|