AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Problems with reading last line of file (https://forums.alliedmods.net/showthread.php?t=40858)

Infra 07-04-2006 23:21

Problems with reading last line of file
 
Code:

// Match stats - Main
// DO NOT EDIT THIS FILE

0
1

Code:
    // Get match number     if( (half == 1) && (main_in2mapmatch != 1) )     {         if( file_size(stats_file_main, 1) > 2 ) // If there are matches in the stats file         {             read_file(stats_file_main, file_size(stats_file_main, 1) - 1, match_num_str, 31, match_num_len)                         stats_match_number = str_to_num(match_num_str) + 1         }         else         {             stats_match_number = 0                     write_file(stats_file_main, "// Match stats - Main")             write_file(stats_file_main, "// DO NOT EDIT THIS FILE")             write_file(stats_file_main, "")         }                 num_to_str( stats_match_number, match_num_str, 31 )         write_file(stats_file_main, match_num_str)     }     else     {               read_file(stats_file_main, file_size(stats_file_main, 1) - 1, match_num_str, 31, match_num_len)                 stats_match_number = str_to_num(match_num_str)     }

The code above is supposed to read the last line of a file (the last match's id) from a file and increment the id for this match. Or if there are no matches in the file, then make the match number = 0.

The problem: Right now 'stats_match_number' will not increment past 1...

Any help would be appreciated...Thanks!

*EDIT* Nevermind O:)...Figured out that write_file writes and extra newline/linefeed to the file, so the file line should be: "file_size(stats_file_main, 1) - 2", instead of: "file_size(stats_file_main, 1) - 1"

jtp10181 07-05-2006 06:48

Re: Problems with reading last line of file
 
you should use the new file natives, they are much mo betta

schnitzelmaker 07-05-2006 07:36

Re: Problems with reading last line of file
 
i use allway this to read every line of file:
Code:

new fsize = file_size(filename,1)
for (new i = 0;i<=fsize;i++){
  read_file(filename,i,Array,32,dummy)
  ...


Hawk552 07-05-2006 11:08

Re: Problems with reading last line of file
 
Quote:

Originally Posted by schnitzelmaker
i use allway this to read every line of file:
Code:

new fsize = file_size(filename,1)
for (new i = 0;i<=fsize;i++){
  read_file(filename,i,Array,32,dummy)
  ...


If you use the new file natives it actually works much better.

jtp10181 07-05-2006 17:49

Re: Problems with reading last line of file
 
not only better but it is MUCH faster, with read file everytime you call it, it opens a new file handle and seeks from the start of the file. With the new natives you open the file handle and then just progress through the file using the same handle.


All times are GMT -4. The time now is 08:01.

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