|
Lite-C file I/O
#258003
03/27/09 05:08
03/27/09 05:08
|
Joined: Jul 2005
Posts: 192 Orange County
silencer
OP
Member
|
OP
Member
Joined: Jul 2005
Posts: 192
Orange County
|
A couple of questions I couldn't find answers to in the manual:
- How does one determine the end of file? (EOF) - Do any of the ANSI I/O work with Lite-C, what do I need to include if it does?
AMD 64 x2 4400+
2048mb DDR3200
NVidia 6800GS 256mb
Soundblaster Audigy 2
A7 Commercial 7.07
|
|
|
Re: Lite-C file I/O
[Re: Xarthor]
#258022
03/27/09 07:42
03/27/09 07:42
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Un-tested, may not want the "=" in the if. if(file_seek(file_handle,0,4)>=file_length(file_handle)) printf("EOF reached");
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Lite-C file I/O
[Re: EvilSOB]
#258035
03/27/09 09:18
03/27/09 09:18
|
Joined: Jul 2005
Posts: 192 Orange County
silencer
OP
Member
|
OP
Member
Joined: Jul 2005
Posts: 192
Orange County
|
Thanks guys, that is helpful. At the moment, I have a file that has strings in a row, for example in test.txt we have: Hello world, Hello my friend The comma is the delimiter. I just want to put those two strings into my string array in the 0 and 1 index. But I'm not sure how to increment through the strings:
while ( (file_str_read(fhandle, test_str)) != -1)
{
file_str_read(fhandle, test_str);
dialog[count] = test_str;
count += 1;
}
The above doesn't seem to work. Admittedly, I haven't been using file IO in A7 very long. Thanks for any help.
AMD 64 x2 4400+
2048mb DDR3200
NVidia 6800GS 256mb
Soundblaster Audigy 2
A7 Commercial 7.07
|
|
|
Re: Lite-C file I/O
[Re: silencer]
#258044
03/27/09 10:25
03/27/09 10:25
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
Serious User
|
Serious User
Joined: Jul 2008
Posts: 1,178
England
|
Hey, use TEXT for storing your strings
TEXT* dialog = {
strings = 2;
}
function get_file(){
int eof = 0;
int count = 0;
while(eof != -1){
eof = file_str_read(fhandle, test_str);
(dialog.pstring)[count] = test_str;
count += 1;
}
}
Hope this helps
|
|
|
Re: Lite-C file I/O
[Re: MrGuest]
#258057
03/27/09 13:22
03/27/09 13:22
|
Joined: Jul 2002
Posts: 4,436 Germany, Luebeck
Xarthor
Expert
|
Expert
Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
|
Take a look at MrGuests code snippet which looks good. Now I'd like to take the time and go through your code:
(1) while ( (file_str_read(fhandle, test_str)) != -1)
(2) {
(3) file_str_read(fhandle, test_str);
(4) dialog[count] = test_str;
(5) count += 1;
(6) }
Line 1: You read from the file stored in fhandle and save it in the String test_str. Remember: test_str is now filled with the first piece of your file until the first delimiter. Now you check if the end of file is reached. Line 3: Again you read from file. So now test_str is filled with the second piece of your file (until the second delimiter, or EOF). And this is the point where your method is failing, because you skip the first piece. So read/use and learn from MrGuest's code snippet.
|
|
|
Re: Lite-C file I/O
[Re: Xarthor]
#258063
03/27/09 14:25
03/27/09 14:25
|
Joined: Jul 2005
Posts: 192 Orange County
silencer
OP
Member
|
OP
Member
Joined: Jul 2005
Posts: 192
Orange County
|
Thanks for the help guys.
function readfile(STRING* filename){
STRING* test_str = "###";
int count;
int eof = 0;
var fhandle;
if ( (fhandle = file_open_read(filename) ) == 0) {
return NULL;
}
fhandle = file_open_read (filename); // Opening file
count = 0;
while(eof != -1){
eof = file_str_read(fhandle, test_str);
(dialog_file.pstring)[count] = test_str;
//dialog[count] = test_str;
count += 1;
}
node1a = (dialog_file.pstring)[1];
file_close(fhandle);
}
Assume there is a text array for "dialog_file" defined already. When I do the code above, when I call node1a, it's the same string (always the last string in the file), no matter what index I call from the text array. For the life of me I can't find where the kink is. The code above seems like it should work.
Last edited by silencer; 03/27/09 14:29.
AMD 64 x2 4400+
2048mb DDR3200
NVidia 6800GS 256mb
Soundblaster Audigy 2
A7 Commercial 7.07
|
|
|
Re: Lite-C file I/O
[Re: Xarthor]
#258067
03/27/09 14:33
03/27/09 14:33
|
Joined: Jul 2005
Posts: 192 Orange County
silencer
OP
Member
|
OP
Member
Joined: Jul 2005
Posts: 192
Orange County
|
That doesn't seem to work either Xarthor. This seems like a serious bug with parsing files in A7, or I really need to stop programming early in the morning.
AMD 64 x2 4400+
2048mb DDR3200
NVidia 6800GS 256mb
Soundblaster Audigy 2
A7 Commercial 7.07
|
|
|
|