AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Resolved] Parsing each line read file? (https://forums.alliedmods.net/showthread.php?t=47947)

hlstriker 11-30-2006 15:48

[Resolved] Parsing each line read file?
 
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?

dutchmeat 11-30-2006 16:13

Re: Parsing each line read file?
 
take a look at this:
http://forums.alliedmods.net/showthr...light=parse%28

[ --<-@ ] Black Rose 11-30-2006 16:16

Re: Parsing each line read file?
 
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); }

The Specialist 11-30-2006 16:22

Re: Parsing each line read file?
 
THREAD HIJACK !

when writeing to a file does the file have to exist already or will it create a file on its own ?:|

[ --<-@ ] Black Rose 11-30-2006 16:23

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

The Specialist 11-30-2006 16:28

Re: Parsing each line read file?
 
thank you +karma:up:

hlstriker 11-30-2006 21:03

Re: Parsing each line read file?
 
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; }

stupok 11-30-2006 21:29

Re: Parsing each line read file?
 
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] }

hlstriker 11-30-2006 21:38

Re: Parsing each line read file?
 
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 12-01-2006 14:41

Re: Parsing each line read file?
 
Does anyone else know how to fix this?


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

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