Raised This Month: $32 Target: $400
 8% 

Solved Select Random Line From The Text File


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 08-24-2020 , 11:13   Select Random Line From The Text File
Reply With Quote #1

I can't do this, This is not working.

Code:
	char path[PLATFORM_MAX_PATH]
	char line[128];
	BuildPath(Path_SM,path, PLATFORM_MAX_PATH, "configs/textfile.txt");
	Handle file = OpenFile(path, "r");
	int iLine = 0;
	while (ReadFileLine(file, line, sizeof(line)))
	{
		iLine++;
	}
	
	CloseHandle(file);
	
	int randomtext = GetRandomInt(1, iLine);
can someone help?

Thanks.

Last edited by EfeDursun125; 06-19-2021 at 18:23.
EfeDursun125 is offline
SSheriFF
AlliedModders Donor
Join Date: May 2020
Location: Israel
Old 08-24-2020 , 11:33   Re: Select Random Line From The Text File
Reply With Quote #2

Quote:
Originally Posted by EfeDursun125 View Post
I can't do this, This is not working.

Code:
	char path[PLATFORM_MAX_PATH]
	char line[128];
	BuildPath(Path_SM,path, PLATFORM_MAX_PATH, "configs/textfile.txt");
	Handle file = OpenFile(path, "r");
	int iLine = 0;
	while (ReadFileLine(file, line, sizeof(line)))
	{
		iLine++;
	}
	
	CloseHandle(file);
	
	int randomtext = GetRandomInt(1, iLine);
can someone help?

Thanks.
Try this.
PHP Code:
while (!IsEndOfFile(file) && ReadFileLine(filelinesizeof(line)))
{
        
iLine++;

__________________
Taking small private requests (Free) and big private requests (Paid).
Contact me via Discord: WilDick#1524

My Plugins:

Last edited by SSheriFF; 08-24-2020 at 11:33.
SSheriFF is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 08-24-2020 , 11:42   Re: Select Random Line From The Text File
Reply With Quote #3

Quote:
Originally Posted by SSheriFF View Post
Try this.
PHP Code:
while (!IsEndOfFile(file) && ReadFileLine(filelinesizeof(line)))
{
        
iLine++;

Not working, Always selects null.
EfeDursun125 is offline
SSheriFF
AlliedModders Donor
Join Date: May 2020
Location: Israel
Old 08-24-2020 , 11:45   Re: Select Random Line From The Text File
Reply With Quote #4

Quote:
Originally Posted by EfeDursun125 View Post
Not working, Always selects null.
Check if your txt file is empty first. If it isnt try this:
PHP Code:
File file OpenFile(path"r");
while(!
IsEndOfFile(file) && file.ReadLine(linesizeof(line)))
{
    
iLine++;

__________________
Taking small private requests (Free) and big private requests (Paid).
Contact me via Discord: WilDick#1524

My Plugins:

Last edited by SSheriFF; 08-24-2020 at 11:50.
SSheriFF is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 08-24-2020 , 12:07   Re: Select Random Line From The Text File
Reply With Quote #5

Quote:
Originally Posted by SSheriFF View Post
Check if your txt file is empty first. If it isnt try this:
PHP Code:
File file OpenFile(path"r");
while(!
IsEndOfFile(file) && file.ReadLine(linesizeof(line)))
{
    
iLine++;

My text file isn't empty but not working.
EfeDursun125 is offline
stephen473
Senior Member
Join Date: Jan 2017
Location: somewhere on earth
Old 08-25-2020 , 08:40   Re: Select Random Line From The Text File
Reply With Quote #6

you may create a ArrayList to store your texts. use PushArrayString to save your texts while reading the file.

to generate a random text:

PHP Code:
GetArrayString(arrayhandleGetRandomInt(0arrayhandle.Length 1), buffertostoretextsizeof(buffertostoretext));
PrintToChatAll("Random text generated from file: %s" buffertostoretext); 
__________________
Also known as Hardy`.

Feel free to contact me if you have a private plugin request!

My Steam Profile | Discord: Hardy`#3792
stephen473 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:00.


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