AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   File Writing....:O (https://forums.alliedmods.net/showthread.php?t=41336)

mysticssjgoku4 07-13-2006 05:53

File Writing....:O
 
The below code reports "Found" but doesn't write the extra line to the file.
No error reported, nothing.
but if it doesn't find the map in the file, it does create the lines as defined in after "if(iFound == 0)".
IT doesn't add the extra snippet lines after the server print though.
Any clue?
Thanks

Code:
            new file_handle = fopen(g_file_path,"rt+")                         new iFound = 0             new temp_string[128]                         while(!feof(file_handle))             {                 new buffer[128]                 fgets(file_handle,buffer,sizeof(buffer))                 if(containi(buffer,g_map_name) != -1)                 {                     iFound = 1                     server_print("Found!")                     format(temp_string,sizeof(temp_string),"^n[Ent] %s %i %i %i %i^n",gName,origin[0],origin[1],origin[2],angle)                     fprintf(file_handle,temp_string)                     break;                 }             }                         if(iFound == 0)             {                 format(temp_string,sizeof(temp_string),"^n[%s]^n[Ent] %s %i %i %i %i^n[/%s]^n",g_map_name,gName,origin[0],origin[1],origin[2],angle,g_map_name)                 fprintf(file_handle,temp_string)             }                         client_print(id,print_console,"[Ent Spawner] Entity %s was saved to file!",gName)             fclose(file_handle)

Greenberet 07-13-2006 07:34

Re: File Writing....:O
 
i am not at home now but i found some errors
maybe this code will fix it


Code:
new file_handle = fopen(g_file_path,"rt+")</p><p>&nbsp;</p><p> new iFound = 0</p><p> static temp_string[128]</p><p>&nbsp;</p><p> while(!feof(file_handle))</p><p> {</p><p> static buffer[128]</p><p> fgets(file_handle,buffer,sizeof(buffer))</p><p> if(containi(buffer,g_map_name) != -1)</p><p> {</p><p> iFound = 1</p><p> server_print("Found!")</p><p> formatex(temp_string,sizeof(temp_string),"^n[Ent] %s %i %i %i %i^n",gName,origin[0],origin[1],origin[2],angle)</p><p> fprintf(file_handle, "%s", temp_string)</p><p> break;</p><p> }</p><p> }</p><p>&nbsp;</p><p> if(iFound == 0)</p><p> {</p><p> formatex(temp_string,sizeof(temp_string),"^n[%s]^n[Ent] %s %i %i %i %i^n[/%s]^n",g_map_name,gName,origin[0],origin[1],origin[2],angle,g_map_name)</p><p> fprintf(file_handle, "%s", temp_string)</p><p> }</p><p>&nbsp;</p><p> client_print(id,print_console,"[Ent Spawner] Entity %s was saved to file!",gName)</p><p> fclose(file_handle)



1) Use formatex instead of format if possible
2) fprintf dont put an non constant string in the format parameter. use fputs or fprintf( file, "%s", mystring )

mysticssjgoku4 07-13-2006 15:11

Re: File Writing....:O
 
Here's the code you posted.
I get two warnings of tag mismatches for the fprintf's.

--Update: And it doesn't save the extra line still.

Code:
fprintf(file_handle, "%s", temp_string)

That's why I was wasn't really using that method. Is there a general reason behind this?

Code:
/* ___________       __  .__  __           \_   _____/ _____/  |_|__|/  |_ ___.__.  |    __)_ /    \   __\  \   __<   |  |  |        \   |  \  | |  ||  |  \___  | /_______  /___|  /__| |__||__|  / ____|         \/     \/               \/       _________                                        /   _____/__________ __  _  ______   ___________  \_____  \\____ \__  \\ \/ \/ /    \_/ __ \_  __ \  /        \  |_> > __ \\     /   |  \  ___/|  | \/ /_______  /   __(____  /\/\_/|___|  /\___  >__|           \/|__|       \/           \/     \/                          - Remo Williams                                |------------------------------------------------------------------------------>.. |--------------------->..Change Log..<----------------------------------------->.. |->..July 5, 2006..<---------------------------------------------------------->.. |------>..Released 1.0..<------------------------------------------------------>.. |------------------------------------------------------------------------------>.. */ #include <amxmodx> #include <amxmisc> #include <engine> #define PLUGIN "Ent Spawner" #define VERSION "1.0" #define AUTHOR "ExtremeKiller" #define CONFIGFILE "entities_spawnable.ini" #define HEADER "//[Ent Spawner] Format:^n//^n//[MapName]^n//[Ent] classname X Y Z ANGLE^n//[/MapName]^n^n" #define MAX_NUM_ENTS 128 #define NUM_VALID_ENTS 27 new g_configs_dir[32] new g_file_path[64] new g_map_name[32] new g_map_string[48] new g_map_string_end[48] new g_ents_valid[NUM_VALID_ENTS][32] = {     "ammo_9mmclip",     "ammo_9mmAR",     "ammo_9mmbox",     "ammo_ARgrenades",     "ammo_buckshot",     "ammo_357",     "ammo_rpgclip",     "ammo_gaussclip",     "ammo_crossbow",     "light",     "light_environment",     "item_longjump",     "item_healthkit",     "item_battery",     "weapon_crowbar",     "weapon_9mmhandgun",     "weapon_357",     "weapon_9mmAR",     "weapon_shotgun",     "weapon_rpg",     "weapon_gauss",     //"weapon_crossbow",//Crashes Game - No Reason     "weapon_egon",     "weapon_tripmine",     "weapon_satchel",     "weapon_handgrenage",     "weapon_snark",     "weapon_hornetgun" } new g_map_found = 0 new g_num_ents = 0 new g_num_ok = 0 new g_num_failed = 0 public plugin_init() {     register_plugin(PLUGIN, VERSION, AUTHOR)     register_concmd("amx_origininfo","origininfo",ADMIN_LEVEL_A," - <0-Show/1-Save> <Location>")     register_concmd("amx_addent","add_ent",ADMIN_LEVEL_A," - <Entity Name> <1-Save/0-NotSave>")     set_task(5.0,"initialize_ents") } public initialize_ents() {     get_configsdir(g_configs_dir,sizeof(g_configs_dir))         format(g_file_path,sizeof(g_file_path),"%s/%s",g_configs_dir,CONFIGFILE)                 file_check()                 get_mapname(g_map_name,sizeof(g_map_name))     format( g_map_string , sizeof( g_map_string ) ,"[%s]", g_map_name )     format( g_map_string_end , sizeof( g_map_string_end ) ,"[/%s]", g_map_name )             new num_lines = file_size(g_file_path,1)         for( new nextline = 0 ; nextline < num_lines ; nextline++ )     {         if(g_num_ents >= MAX_NUM_ENTS)         {             server_print("[Ent Spawner] Sorry, you have too many entities.")             return PLUGIN_HANDLED         }                 new i_Num_Chars         new s_text[256]                 read_file(g_file_path , nextline , s_text , sizeof( s_text ) , i_Num_Chars )                 if( equali ( s_text , g_map_string ) && g_map_found == 0)         {             server_print( "[Ent Spawner] Settings for this map were detected.")             g_map_found = 1         }                 switch (g_map_found)         {               case 1:             {                 if( containi(s_text,"[Ent]") != -1 )                 {                     g_num_ents++                     parse_line(s_text)                 }                 else                 if(equali(s_text,g_map_string_end)) //Map End                 {                     g_map_found = 0                     break                 }             }         }     }         server_print("[Ent Spawner] Number of Entities | Found: %i | OK: %i | BAD: %i",g_num_ents,g_num_ok,g_num_failed)     return PLUGIN_HANDLED } /*     [Ent] classname X Y Z ANGLE */ public parse_line(string[]) {       new arg1[24],arg2[24],arg3[24],arg4[24],arg5[24],arg6[24]         parse(string,arg1,sizeof(arg1),arg2,sizeof(arg2),arg3,sizeof(arg3),arg4,sizeof(arg4),arg5,sizeof(arg5),arg6,sizeof(arg6))         new x = str_to_num(arg3)     new y = str_to_num(arg4)     new z = str_to_num(arg5)     new angle = str_to_num(arg6)         ent_spawn(arg2,x,y,z,angle)     return 1; } public ent_spawn(SZ_ent_name[],x,y,z,angle) {     server_print("Name: %s | X: %i | Y: %i | Z: %i | Angle: %i",SZ_ent_name,x,y,z,angle)     new Float:originF[3]     new Float:angles[3] = { 0.0, 0.0, 0.0 }         new ent = create_entity(SZ_ent_name)         //Configure Origin     originF[0] = float(x)     originF[1] = float(y)     originF[2] = float(z)         //Configure Angle     angles[1] = float(angle)         //Set Origin     entity_set_origin(ent,originF)         //Set Angle     entity_set_vector(ent,EV_VEC_angles,angles)         //Set Class Name     entity_set_string(ent,EV_SZ_classname,SZ_ent_name)         DispatchSpawn(ent)         g_num_ok++     return PLUGIN_HANDLED } public add_ent(id) {     if(!is_user_alive(id)) return PLUGIN_HANDLED         new gName[32], gSave[32]         read_argv(1,gName,sizeof(gName))     read_argv(2,gSave,sizeof(gSave))     new iSave = str_to_num(gSave)         new origin[3]     get_user_origin(id,origin)         new angle = get_user_angle(id)         if(!gName[0] || is_valid_entname(gName) == 0)     {         client_print(id,print_console,"[Ent Spawner] Invalid entity name. Usage: amx_addent <Ent Name> <1-Save/0-NotSave>")     }     else     {         if(iSave >= 1)         {             file_check()                         new file_handle = fopen(g_file_path,"rt+")             new iFound = 0             static temp_string[128]             while(!feof(file_handle))             {                 static buffer[128]                 fgets(file_handle,buffer,sizeof(buffer))                 if(containi(buffer,g_map_name) != -1)                 {                       iFound = 1                     server_print("Found!")                     formatex(temp_string,sizeof(temp_string),"^n[Ent] %s %i %i %i %i^n",gName,origin[0],origin[1],origin[2],angle)                     fprintf(file_handle,temp_string)                     break;                 }             }             if(iFound == 0)             {                 formatex(temp_string,sizeof(temp_string),"^n[%s]^n[Ent] %s %i %i %i %i^n[/%s]^n",g_map_name,gName,origin[0],origin[1],origin[2],angle,g_map_name)                 fprintf(file_handle,temp_string)             }             client_print(id,print_console,"[Ent Spawner] Entity %s was saved to file!",gName)             fclose(file_handle)         }         else         {             client_print(id,print_console,"[Ent Spawner] Entity %s was not saved to file.",gName)         }         ent_spawn(gName,origin[0],origin[1],origin[2],angle)     }     return PLUGIN_HANDLED } //Displays Origin Information Including Angle public origininfo(id) {     if(!(get_user_flags(id) & ADMIN_LEVEL_A))     {         client_print(id, print_console, "[AMXX] You do not have access to this command!^n")         return PLUGIN_HANDLED     }         new arg[32]     read_argv(1,arg,31)         new arg2[32]     read_argv(2,arg2,31)         new mode = str_to_num(arg)         new origin[3]     get_user_origin(id,origin)         new origin_format[128]     format(origin_format,sizeof(origin_format),"Origin: | X: %i | Y: %i | Z: %i | Angle: %i | Location: %s |",origin[0],origin[1],origin[2],get_user_angle(id),arg2)         switch (mode)     {         case 1:         {             if(!arg2[0])             {                 client_print(id,print_console,"[OriginExport] Please specify a location name to export,")                 return PLUGIN_HANDLED             }             log_to_file("Extreme_Exported_Origins.txt",origin_format)             client_print(id,print_console,"[OriginExport] Origin successfully exported to file.")         }     }         client_print(id,print_console,origin_format)     return PLUGIN_HANDLED } stock is_valid_entname(gName[]) {     for(new i=0; i < NUM_VALID_ENTS; i++)     {         if(equali(gName,g_ents_valid[i])) return 1;     }     return 0; } //Gets angle the user is looking. stock get_user_angle(id) {     new Float:vec[3]     entity_get_vector(id,EV_VEC_angles,vec)         new angle = floatround(vec[1])         return angle } public file_check() //Return 0 on creation - Return 1 on exists {     if(!file_exists(g_file_path))     {         write_file(g_file_path,HEADER,-1)         server_print("[Ent Spawner] Config File %s was successfully created.",CONFIGFILE)     }     else     {         server_print("[Ent Spawner] Config File %s was successfully found!",CONFIGFILE)         return 1;     }     return 0; }

Hawk552 07-13-2006 21:03

Re: File Writing....:O
 
first off, you didn't check if file_handle is valid (use if(!file_handle) { return } or whatever) then on top of that you open it with flags "rt+" which don't even exist. Use r,w,or a.

mysticssjgoku4 07-13-2006 23:46

Re: File Writing....:O
 
Quote:

Originally Posted by Hawk552
first off, you didn't check if file_handle is valid (use if(!file_handle) { return } or whatever) then on top of that you open it with flags "rt+" which don't even exist. Use r,w,or a.

How doesn't rt+ exist?

Quote:

Returns 0 on failure.

Mode uses the standard C library of mode types.
The first character can be:
"a" - append
"r" - read
"w" - write

The second character can be:
"t" - text
"b" - binary

You can also append a "+" to specify both read and write.

It obviously does if it writes to the file after reading it when the map name isn't found in the settings.

----

BTW, the file_handle returns positive. It's not invalid.

mysticssjgoku4 07-15-2006 12:21

Re: File Writing....:O
 
Any ideas anyone?


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

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