PDA

View Full Version : Reading a file


AzcariaX
09-22-2009, 12:09
Hi!

This is my code:


public get_baa(id)
{
new name[60]
new number
new i
new read=1

while(read!=0)
{
read=read_file("buyadmin.txt", i, name, 60, number)
ColorChat(id, GREEN, "Name: %s", name)
i++
}
return PLUGIN_HANDLED
}


I know there is a better way to do this but i can't figure it out. This way is pretty buggy. It types out the first line two times. Please help me.

Exolent[jNr]
09-22-2009, 14:00
You should use the new file natives.
public get_baa(id)
{
new iFile = fopen( "buyadmin.txt", "rt" );
if( iFile )
{
static szData[ 32 ];
while( !feof( iFile ) )
{
fgets( iFile, szData, 31 );
// fgets( ) also gives the new line character '^n' at the end of the string if there is another line
// you don't want that in your message, so this removes it.
trim( szData );
ColorChat(id, GREEN, "Name: %s", szData)
}

fclose( iFile );
}
return PLUGIN_HANDLED;
}

Alka
09-22-2009, 14:39
You can easily use trim() native.

fysiks
09-22-2009, 14:41
You could use trim() to get rid of the new line character(s).

Exolent[jNr]
09-22-2009, 14:45
I wasn't aware that trim( ) also did new line characters and wasn't limited to spaces.