AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   C file natives (https://forums.alliedmods.net/showthread.php?t=46283)

FatalisDK 10-22-2006 19:41

C file natives
 
I want to convert this to the C file natives (fopen, fseek, fgets, etc)
And yes, I know this plugin doesnt handle any errors, it's just a rough draft.

Code:
#include <amxmodx> #define PLUGIN "New Plugin" #define VERSION "0.1" #define AUTHOR "FatalisDK" new const gFILE[] = "test.txt"; public plugin_init() {     register_plugin(PLUGIN, VERSION, AUTHOR);     test(); } test() {     //Display last 10 lines of file - File is always greater than 10 lines     //How would I do this using the C file natives? (or whatever you call them)         new lines = file_size(gFILE, 1);     new szData[3], txtLen         for( new i = lines-10; i < lines; i++ )     {                 read_file(gFILE, i, szData, 2, txtLen);         server_print("%i = %s", i, szData);     } }

Zenith77 10-22-2006 21:21

Re: C file natives
 
The code I gave you was fine; the only problem with it was I don't know how to go back 10 lines lol.

MaximusBrood 10-23-2006 05:11

Re: C file natives
 
This works, but it's not very efficient. I won't recommend you to use it.
If I only knew how to tell the filePointer to move a line further without using fgets().

Code:
#include <amxmodx> #include <amxmisc> #pragma semicolon 1 new g_file[64]; public plugin_init() {     register_plugin("File Natives Test", "x", "MaximusBrood");         get_configsdir(g_file, 63);     format(g_file, 63, "%s/test.txt", g_file);         register_srvcmd("testMe", "readFile"); } public readFile() {     //Note that this is very ugly     //I don't really know how to use fseek()     if(!file_exists(g_file))         return;             //Open file     new filePointer = fopen(g_file, "rt");     if(!filePointer)         return;             //Get the text     new targetLine = file_size(g_file, 1) - 10;         static buffer[128]; new b = 0;     for(new a = 0; !feof(filePointer) && b <= 10; a++)     {         fgets(filePointer, buffer, 127);                 if(a < targetLine)             continue;                 server_print("%d = %s", b, buffer);                 b++;     } }


All times are GMT -4. The time now is 04:52.

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