Raised This Month: $ Target: $400
 0% 

Help reading file and date


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
TudorLaLeagane
Junior Member
Join Date: Mar 2018
Old 01-06-2019 , 18:06   Help reading file and date
Reply With Quote #1

Hi! I want to read more dates from a file, and then check if that date is today.

I don't really have a code now, idk if that is a problem... I was tasked with this today, and i am trying to figure out how to read multiple lines and check the date. If i need a code first, sorry.
TudorLaLeagane is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-06-2019 , 23:18   Re: Help reading file and date
Reply With Quote #2

What do you mean that you were "tasked with this today"? Is this some sort of homework?

If not, then you should try to do it and post the code that you tried so that we can give you advice.
__________________
fysiks is offline
TudorLaLeagane
Junior Member
Join Date: Mar 2018
Old 01-07-2019 , 06:01   Re: Help reading file and date
Reply With Quote #3

No, it's not like a homework, it is for a server in my comunity...
TudorLaLeagane is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-07-2019 , 09:13   Re: Help reading file and date
Reply With Quote #4

Check what date? Where is the date? Give an example of how the lines in the file look like. We can't use crystal balls here.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
TudorLaLeagane
Junior Member
Join Date: Mar 2018
Old 01-07-2019 , 16:07   Re: Help reading file and date
Reply With Quote #5

Code:
public CheckFile()
{
	new dir[33]
	get_configsdir(dir,charsmax(dir))
	new FILE[65]
	format(FILE,charsmax(FILE),"%s/LaLeaganeGather.txt",dir)
	if(!file_exists(FILE))
	{
		write_file(FILE,"; Exemplu adaugare data: 08/01/2019",0)
		write_file(FILE,"; Pentru a adauga un interval orar, scrie doar ora inceperii gather-ului.",1)
		write_file(FILE,"; Exemplu: Linia 1 08/01/2019 Linia 2 19:30",2)
		write_file(FILE,"; Echipa LaLeagane va ureaza distractie placuta tuturor!")
		log_amx("Nu am gasit fisierul pentru Gather, asa ca am creat noi unul!")
		return PLUGIN_HANDLED
	}
	new ReadData = 0
	new Dataitem = 0
	new Houritem = 0
	new f = fopen(FILE,"r")
	while(!feof(f))
	{
		new szLine[17]
		fgets(f,szLine,charsmax(szLine))
		if(!szLine[0] || szLine[0] == ';')
		{
			continue;
		}
		if(Dataitem > 2 || Houritem > 2)
		{
			log_amx("Ai introdus mai mult de 3 dati in fisierul LaLeaganeGather")
		}
		if(ReadData == 0)
		{
			format(GatherData[Dataitem],16,"%s",szLine)
			Dataitem += 1
			ReadData = 1
		}
		else
		{
			format(GatherOra[Houritem],16,"%s",szLine)
			Houritem += 1
			ReadData = 0
		}
	}
	// DEBUG
	for(new i = 0; i < 3; i++)
	{
		log_amx("%s - Data",GatherData[i])
		log_amx("%s - Ora",GatherOra[i])
	}
	return PLUGIN_HANDLED
}

here is the File i have, this part works, but i don't know how to take current server date...

EDIT: Here is the code that is checking the dates, but, somehow, it says that the strings don't match...
Code:
public CheckDateAndTime()
{
	new DataCurenta[16] 
	get_time("%d/%m/%Y",DataCurenta,charsmax(DataCurenta))
	log_amx("%s",DataCurenta)
	for(new i = 0; i < 3; i++)
	{
		log_amx("%s - %d",GatherData[i],i)
		if(strcmp(GatherData[i],DataCurenta) == 0)
		{
			log_amx("1")
		}
		else
		{
			log_amx("0")
		}
	}
}
Here is the file:
Code:
07/01/2019
5995:4000
09/01/2019
6000

Last edited by TudorLaLeagane; 01-07-2019 at 16:46.
TudorLaLeagane is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-07-2019 , 18:32   Re: Help reading file and date
Reply With Quote #6

Provide the GatherData[] array declaration. Your code looks overly complicated for what I think you're trying to do. Can you explain more what your intending to do (do not explain in code).

Also, you could store the time/date as a 4-byte unix timestamp and simplify this:
1546903919 = 01/07/2019 @ 11 : 31pm
__________________

Last edited by Bugsy; 01-07-2019 at 18:32.
Bugsy is offline
TudorLaLeagane
Junior Member
Join Date: Mar 2018
Old 01-08-2019 , 05:55   Re: Help reading file and date
Reply With Quote #7

Code:
new GatherData[3][17]
It's a global variable

So, i want to read a date from a file (MAX is 3) and if that date is today, the plugin should do something.

Last edited by TudorLaLeagane; 01-08-2019 at 06:03.
TudorLaLeagane is offline
thEsp
BANNED
Join Date: Aug 2017
Old 01-08-2019 , 11:38   Re: Help reading file and date
Reply With Quote #8

Quote:
Originally Posted by TudorLaLeagane View Post
Code:
new GatherData[3][17]
It's a global variable

So, i want to read a date from a file (MAX is 3) and if that date is today, the plugin should do something.
Do you think only with that code we can help you? Post all the code. In this case it is necessary...
Also it would be better if you could translate that code. At least we can know what plugin you want to make ...
thEsp is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-08-2019 , 13:11   Re: Help reading file and date
Reply With Quote #9

Work with timestamps. Read the date from file and use parse_time on it, this will return the corresponding timestamp. Then, use get_systime to get the current time stamp and compare this two.
__________________
HamletEagle is offline
TudorLaLeagane
Junior Member
Join Date: Mar 2018
Old 01-08-2019 , 14:45   Re: Help reading file and date
Reply With Quote #10

Quote:
Originally Posted by HamletEagle View Post
Work with timestamps. Read the date from file and use parse_time on it, this will return the corresponding timestamp. Then, use get_systime to get the current time stamp and compare this two.
Let me try real quick
Edit: It still doesn't work.

Here is the full plugin:
Code:
#include <amxmodx>
new GatherData[3][17]
new GatherOra[3][17]
public plugin_init()
{	
	register_plugin("Test", "0.1", "ATudor");
	set_task(0.3,"CheckFile",5000)
	set_task(1.0,"CheckDateAndTime",7000)
}
public CheckFile()
{
	new dir[33]
	get_configsdir(dir,charsmax(dir))
	new FILE[65]
	format(FILE,charsmax(FILE),"%s/LaLeaganeGather.txt",dir)
	if(!file_exists(FILE))
	{
		write_file(FILE,"; Exemplu adaugare data: 08/01/2019",0)
		write_file(FILE,"; Pentru a adauga un interval orar, scrie doar ora inceperii gather-ului.",1)
		write_file(FILE,"; Exemplu: Linia 1 08/01/2019 Linia 2 19:30",2)
		write_file(FILE,"; Echipa LaLeagane va ureaza distractie placuta tuturor!")
		log_amx("Nu am gasit fisierul pentru Gather, asa ca am creat noi unul!")
		return PLUGIN_HANDLED
	}
	new ReadData = 0
	new Dataitem = 0
	new Houritem = 0
	new f = fopen(FILE,"r")
	while(!feof(f))
	{
		new szLine[17]
		fgets(f,szLine,charsmax(szLine))
		if(!szLine[0] || szLine[0] == ';')
		{
			continue;
		}
		if(Dataitem >= 3 || Houritem >= 3)
		{
			log_amx("Ai introdus mai mult de 3 dati in fisierul LaLeaganeGather")
			break;
		}
		if(ReadData == 0)
		{
			parse_time(szLine,"%d/%m/%Y",-1)
			format(GatherData[Dataitem],16,"%d",szLine)
			Dataitem += 1
			ReadData = 1
		}
		else
		{
			format(GatherOra[Houritem],16,"%s",szLine)
			Houritem += 1
			ReadData = 0
		}
	}
	fclose(f)
	return PLUGIN_HANDLED
}
public CheckDateAndTime()
{
	new Datacurenta[17]
	format(Datacurenta,16,"%d",get_systime(0))
	//get_time("%d/%m/%Y",DataCurenta,charsmax(DataCurenta))
	log_amx("%d - systime",get_systime(0))
	log_amx("%s - Datacurenta",Datacurenta)
	for(new i = 0; i < 3; i++)
	{
		log_amx("%s - %d",GatherData[i],i)
		if(strcmp(GatherData[i],Datacurenta) == 0)
		{
			log_amx("Datile corespund!")
		}
		else
		{
			log_amx("Datile nu corespund!")
		}
	}
}

Last edited by TudorLaLeagane; 01-08-2019 at 15:08.
TudorLaLeagane is offline
Reply



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 07:34.


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