Singular https://www.singular.uni-kl.de/forum/ |
|
Printing date/time in the output https://www.singular.uni-kl.de/forum/viewtopic.php?f=10&t=1850 |
Page 1 of 1 |
Author: | vinay [ Mon Aug 23, 2010 1:49 pm ] |
Post subject: | Printing date/time in the output |
I want to print the date & time in the output file. I tried various methods. I could get a working solution, but can somebody suggest me a better alternative? These are the things I have tried: 1. Code: write(lnk,system ("sh","date")); Here lnk is a writable link to a text file. This just prints the output of the system command to the link. 2. Then I tried to write to the file using shell redirections. Code: write(lnk,system ("sh","date > a")); OR write(lnk,system ("sh","date >> a")); This really works... But only if the file is not open! i.e. if the file is opened by some link, then this wont work. So the only working solution I could find is: Code: link lnk=":a file.txt" // Note the :a part. The link should be appendable, not just writable. open(lnk); do_your_process; close(lnk); write(lnk,system ("sh","date >> a")); // Note the >> redirection. If you put just one > then it will erase the whole // file and just put the date. open(lnk); do_remaining_proces; close(lnk); Thanks and regards VInay |
Author: | vinay [ Wed Aug 25, 2010 8:47 am ] |
Post subject: | Re: Printing date/time in the output |
Essentially the question boils down to: How to get the output of the following command in string: Code: system("sh","date") -- VInay |
Author: | gorzel [ Wed Aug 25, 2010 3:38 pm ] |
Post subject: | Re: Printing date/time in the output |
To answer your questions: 1.) The command system returns the errorcode of the shell command, where 0 means that no error occured. So Code: write(lnk,system ("sh","date")); will only write a 0 to the link. 2.) By construction links in Singular bhave very rigid. Once you open an ascii-file by a link command, you can not acess it simultaneously by >. Since you only want to write the data to an ASCII file, you have two possibilities. Either, you don't use link but access the file simply by its filename with preceeding > (to open) and >> (to append): Code: string lnkname = "file.dat"; write(">"+lnkname,"123"); // creates and write to file.date write(">>"+lnkname,"hello"); // appends to the file int errcode = system("sh","date >> " + lnkname); // append the date to file.dat write(">>"+lnkname,"the end"); read(lnkname); // returns the whole file as a string OR work constantly work with link but to get the date as a string, you have to write it to a file, then read this file into a string, and finally remove the file to clean the filesystem. Here is proc which does the job: Code: proc timestamp() { // returns the date as a string int errcode; string fnm = "/tmp/date"+string(system("pid"))+".sig"; // create unique name errcode = system("sh","date > "+fnm); string date = read(fnm); date[size(date)]=""; // remove endining newline errcode = system("sh","rm -f "+fnm); // remove the date-file return(date); } > timestamp(); Mi 25. Aug 15:21:10 CEST 2010 Then you can proceed as expceted: Code: link lnk=":a file.txt"; // Note the :a part. The link is appendabble not just writable.
open(lnk); // do_your_process; write(lnk,timestamp()); // write the date to the file // do_remaining_proces; close(lnk); |
Author: | vinay [ Thu Aug 26, 2010 5:46 am ] |
Post subject: | Re: Printing date/time in the output |
Bingo!!! This timestamp() procedure works perfectly fine... This is what I was looking for!!! I wonder how come it didnt strike to me to read the file into a string to which I was already writing Anyways, thanks a lot! -- VInay |
Author: | vinay [ Thu Aug 26, 2010 5:52 am ] |
Post subject: | Re: Printing date/time in the output |
Also this write command with ">" and ">>" redirections is really a great idea. I had never used/noticed it before! VInay |
Author: | seelisch [ Fri Aug 27, 2010 10:24 am ] |
Post subject: | Re: Printing date/time in the output |
There will be a new command in the next minor SINGULAR release called 'datetime' which will return a string of the format Www Mmm dd hh:mm:ss yyyy where Www is the weekday, Mmm the month in letters, dd the day of the month as digits, hh:mm:ss the time, and yyyy the year. I thought it a good idea to have this, as the above proposed workaround is a bit involved, and there actually is a C function which just needed to be pushed through the SINGULAR interpreter... Usage will be > datetime; or > string s = datetime; Regards, Frank |
Author: | vinay [ Fri Aug 27, 2010 3:22 pm ] |
Post subject: | Re: Printing date/time in the output |
Dear Frank, Pushing date() function into Singular is certainly a good idea. In fact thinking it more generally, and from the prospective feature point of view, will it be possible to "push" any C function through Singular? I dont know whether how to implement this technically. But sometime I find that some code can be executed with C better that Singular. The workaround for that I do is Code: system ("sh","compile_and_execute_batch_script"); But then doing this for small code is bit tedious... -- VInay |
Author: | hannes [ Fri Sep 24, 2010 6:43 pm ] |
Post subject: | Re: Printing date/time in the output |
datetime() is now part of standard.lib - we will not implement anything which can be easily done in a library. For adding your own C/C++ function to Singular see http://www.singular.uni-kl.de/DynMod.ps |
Page 1 of 1 | All times are UTC + 1 hour [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |