Reading EVERYTHING from a file -
Wesley221 - 10.09.2011
Hey,
How can i read every single word that is in a file, and then use it as a variable?
Currecly im just using one variable, where i store all the forbidden names in. But i want it so it reads it from a file and then check if its in someones name.
I already got it working with the variable, so now i just need to know how to store everything that is in the file in the variable.
If someone could give me a push in the right direction (wiki link, maybe a FS that uses this or maybe an example), i would be thankfull
~Wesley
Re: Reading EVERYTHING from a file -
Darnell - 10.09.2011
File Functions
Try using
io_read
Re: Reading EVERYTHING from a file -
Wesley221 - 10.09.2011
Ah thanks, i will try this
Re: Reading EVERYTHING from a file -
Basicz - 10.09.2011
pawn Code:
new File: fhandler = fopen( "nyancat.languages", io_read );
new string[ 128 ]; // string which will be stored while reading
while ( fread( fhandler, string ) )
{
if ( !strcmp( string, "English", true ) ) NyanForAllPlayers( LANG_ENGLISH );
else if ( !strcmp( string, "Japanese", true ) ) NyanForAllPlayers( LANG_JAPAN );
}
Well hope it can help you.
Re: Reading EVERYTHING from a file -
Wesley221 - 10.09.2011
So it will basicly store every word that is inside the 'nyancat.languages' folder, and then add it to the string?
Re: Reading EVERYTHING from a file -
Wesley221 - 10.09.2011
Sorry for doublepost, but its not working..
pawn Code:
new File: FName = fopen( "Forbidden/ForbiddenNames.txt", io_read );
new ForbiddenNames[sizeof(FName)];
while ( fread( FName, ForbiddenNames ) )
{
printf(ForbiddenNames);
/*if( strfind( ForbiddenNames, GetName(playerid), true ) != -1 )
{*/
if( !strcmp(GetName(playerid), ForbiddenNames, true ) )
{
SendClientMessage(playerid, COLOR_ORANGE, " ** We found a forbidden word in your name! Please remove it and re-connect. ");
Kick(playerid);
}
//}
}
I did it with strfind, strcmp, but both didnt work. I tried to log in as 'cunt' (is in the forbiddennames file), and didnt got kicked.
It did print all the things that were in the ForbiddenNames variable.
Code:
[17:19:47] naab
[17:19:47] nab
[17:19:47] fuck
[17:19:47] fack
[17:19:47] fak
[17:19:47] fuk
[17:19:47] gay
[17:19:47] lesbian
[17:19:47] lesb
[17:19:47] shemale
[17:19:47] fag
[17:19:47] faggot
[17:19:47] fucker
[17:19:47] dick
[17:19:47] fat
[17:19:47] asshole
[17:19:47] ass
[17:19:47] cunt
[17:19:47] cancer
[17:19:47] kanker
[17:19:47] klootzak
[17:19:47] kut
[17:19:47] lul
[17:19:47] eikel
[17:19:47] flikker
[17:19:47] sukkel
[17:19:47] sucker
[17:19:47] wanker
What can the problem be?
Edit: I just noticed it did NOT print the names in from the file with "print(ForbiddenNames);".
Edit #2: I just fixed the problem, it was because of 'new ForbiddenNames[sizeof(FName)];', the sizeof thing. Is it possible to get the amount of letters that are inside a file, or do i have to create a big variable to store all the names in?
Respuesta: Re: Reading EVERYTHING from a file -
OPremium - 10.09.2011
pawn Code:
new File:FName
= fopen("Forbidden/ForbiddenNames.txt", io_read
);
new ForbiddenNames
[100];
while(fread(FName, ForbiddenNames
)) //Reads the file line-by-line -> https://sampwiki.blast.hk/wiki/Fread{ //printf(ForbiddenNames); strdel(ForbiddenNames,
strfind(ForbiddenNames,
"\r\n", false
),
strfind(ForbiddenNames,
"\r\n", false
)+4));
//Deletes the "New line" (\r\n) from the string if(strfind(GetName
(playerid
), ForbiddenNames, true
) != -1) //Searchs the forbidden word { SendClientMessage
(playerid, COLOR_ORANGE,
" ** We found a forbidden word in your name! Please remove it and re-connect. ");
Kick
(playerid
);
//Kick xD }}fclose(FName
);
Try using that code
Re: Reading EVERYTHING from a file -
Wesley221 - 10.09.2011
In the post above i said its already solved, but one thing isnt.
How can i get the amount of all letters that are inside the file or isnt this possible, so i just have to create a big variable? (Note: You can add names with a command, thats y i want to know this)
Also, with this version:
pawn Code:
new File: FName = fopen( "Forbidden/ForbiddenNames.txt", io_read );
new ForbiddenNames[300];
while ( fread( FName, ForbiddenNames ) )
{
print(ForbiddenNames);
if( strfind( ForbiddenNames, GetName(playerid), true ) != -1 )
{
pShowLM[playerid] = false;
SendClientMessage(playerid, COLOR_ORANGE, " ** We found a forbidden word in your name! Please remove it and re-connect. ");
Kick(playerid);
}
}
And i log in as 'CuntL', im not getting kicked. Im telling it to ignore the higher cases (so it doesnt matter if your name is uppercase or lowercase), it should kick you. And since 'CuntL' contains 'Cunt', i should be kicked: why am im not getting kicked?
Re: Reading EVERYTHING from a file -
[M]onsieur - 10.09.2011
pawn Code:
public OnPlayerConnect(playerid)
{
static string[MAX_PLAYER_NAME + 5];
new File:archive = fopen("Kicked.txt", io_read);
while(fread(archive, string)) {
string[strlen(string)-2] = EOS; //remove \r\n form file
if(!strfind(string, GetPlayerNick(playerid), true)) {
Kick(playerid);
}
}
fclose(archive);
return 1;
}
GetPlayerNick(i)
{
static nick[24];
return GetPlayerName(i, nick, 24), nick;
}
Re: Reading EVERYTHING from a file -
Sasino97 - 10.09.2011
To store separated words by a white space you can use strtok