Raised This Month: $ Target: $400
 0% 

[Resolved] Parsing each line read file?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hlstriker
Green Gaben
Join Date: Mar 2006
Location: OH-IO!
Old 11-30-2006 , 15:48   [Resolved] Parsing each line read file?
Reply With Quote #1

Hi I am trying to read each line of my file, then put each separate line into a slot in an array.

Here is the code I am trying to use...
Code:
        new readdata[128];         new parsedline;         new newOrigin[6];                 new i = 0;         while(fgets(filepointer,readdata,127))         {             parse(readdata, parsedline, 31);             newOrigin[i] = parsedline;             i ++;         }

The file contains 6 lines. I want each line into the newOrigin array. How do I do this?

Last edited by hlstriker; 12-01-2006 at 23:31. Reason: Resolved
hlstriker is offline
dutchmeat
Senior Member
Join Date: Sep 2006
Old 11-30-2006 , 16:13   Re: Parsing each line read file?
Reply With Quote #2

take a look at this:
http://forums.alliedmods.net/showthr...light=parse%28
__________________
before you criticize someone, you should walk a mile in their shoes. that way, when you criticize them, you're a mile away and you have their shoes.
dutchmeat is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 11-30-2006 , 16:16   Re: Parsing each line read file?
Reply With Quote #3

Code:
func() {         if ( ! file_exists(filename) )         return -1;         new file = fopen(filename, "r");         new data[128];     new newOrigin[6];         while( ! feof(file) ) {                 fgets(file, data, 127);                     newOrigin[i] = str_to_num(data);                 i++;     }         fclose(file); }
[ --<-@ ] Black Rose is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 11-30-2006 , 16:22   Re: Parsing each line read file?
Reply With Quote #4

THREAD HIJACK !

when writeing to a file does the file have to exist already or will it create a file on its own ?
The Specialist is offline
Send a message via AIM to The Specialist
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 11-30-2006 , 16:23   Re: Parsing each line read file?
Reply With Quote #5

write_file() creates a file if it doesn't exist.
EDIT:I think all the new natives do the same, at least fprintf() does.

Last edited by [ --<-@ ] Black Rose; 12-11-2006 at 11:37.
[ --<-@ ] Black Rose is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 11-30-2006 , 16:28   Re: Parsing each line read file?
Reply With Quote #6

thank you +karma
The Specialist is offline
Send a message via AIM to The Specialist
hlstriker
Green Gaben
Join Date: Mar 2006
Location: OH-IO!
Old 11-30-2006 , 21:03   Re: Parsing each line read file?
Reply With Quote #7

Alright, now when this trys to load the lines from the file they all get set to zero (0) for some reason. The file its loading from has 6 lines. Lines 1-3 are the origin coords for one entity, and lines 4-6 are the origin coords for another entity. I am trying to load the coords from this file but they always get set to zero and my entities spawn at 0,0,0 and 0,0,0.

Here is the exact code i'm using...
Code:
public readTimer(id) {     new file = fopen(filename, "r");     if(file)     {         new data[128];         new newOrigin[6];         new i;                 while(!feof(file))         {             fgets(file, data, 127);             newOrigin[i] = str_to_num(data);             i++;         }                 if(!startCreated)         {             // Load the origins from file             new Float:newStartOrigin2[3];             newStartOrigin2[0] = str_to_float(newOrigin[0]);             newStartOrigin2[1] = str_to_float(newOrigin[1]);             newStartOrigin2[2] = str_to_float(newOrigin[2]);                         // ALOT OF CODE NOT NEEDED REMOVED SO ITS NOT SO LONG!             entity_set_origin(startEnt, newStartOrigin2);             // ALOT OF CODE NOT NEEDED REMOVED SO ITS NOT SO LONG!         }                 if(!endCreated)         {             // Load the origins from file             new Float:newEndOrigin2[3];             newEndOrigin2[0] = str_to_float(newOrigin[3]);             newEndOrigin2[1] = str_to_float(newOrigin[4]);             newEndOrigin2[2] = str_to_float(newOrigin[5]);                         // ALOT OF CODE NOT NEEDED REMOVED SO ITS NOT SO LONG!             entity_set_origin(endEnt, newEndOrigin2);             // ALOT OF CODE NOT NEEDED REMOVED SO ITS NOT SO LONG!             }     }     fclose(file);         return PLUGIN_CONTINUE; }
hlstriker is offline
stupok
Veteran Member
Join Date: Feb 2006
Old 11-30-2006 , 21:29   Re: Parsing each line read file?
Reply With Quote #8

Nevermind, I guess I still don't understand how the new file natives work. I'd wager my attempt is incorrect as well.

The 'i' isn't defined nor is it necessary. And this will not work, as it will read the first 128 characters of text and assign it to newOrigin[i]. That is not correct.
Code:
while(!feof(file))         {             fgets(file, data, 127);             newOrigin[i] = str_to_num(data);             i++         }

This will not provide the functionality you are looking for, but as far as I know, it is correct. I never caught up with the new file natives, I still use the old ones.
Code:
while(!feof(file)) {     fgets(file, data, 127)     new i = ftell(file)     newOrigin[i] = data[i] }

Last edited by stupok; 11-30-2006 at 22:15.
stupok is offline
hlstriker
Green Gaben
Join Date: Mar 2006
Location: OH-IO!
Old 11-30-2006 , 21:38   Re: Parsing each line read file?
Reply With Quote #9

When I use that code my console tells me to debug and it does not work at all. I really have no clue what to fix here because I still don't really understand why my code didn't work, let alone yours.
hlstriker is offline
hlstriker
Green Gaben
Join Date: Mar 2006
Location: OH-IO!
Old 12-01-2006 , 14:41   Re: Parsing each line read file?
Reply With Quote #10

Does anyone else know how to fix this?
hlstriker is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:56.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode