Jump to content

Wikipedia:Reference desk/Archives/Computing/2015 November 21

From Wikipedia, the free encyclopedia
Computing desk
< November 20 << Oct | November | Dec >> November 22 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


November 21

[edit]

reading / writing a file with javascript

[edit]

Hi, I'm trying to write a simple game in html/css/javascript and want to be able to read / write the score, etc to/from a file, is this at all possible? I know that visualbasic scripting has Scripting.FileSystemObject. Any help/comment would be appreciated.

(ps. I have no option to google or download stuff from other sites.) — Preceding unsigned comment added by Bejacobs (talkcontribs) 11:01, 21 November 2015 (UTC)[reply]

Javascript in a web browser doesn't have untrammelled access to the user's file system, as this would be a massive security problem. The new File API allows users to grant limited access to some pages, although (because it's new) support for it is patchy. If you just want persistent storage on the client (e.g. to save state, which can be recovered the next time someone plays the game) you can use the localstorage API or even just store what you need in a cookie. -- Finlay McWalterTalk 11:09, 21 November 2015 (UTC)[reply]
If, on the other hand, you were writing in Javascript but on the server rather than the client, environments like NodeJS have a filesystem API. -- Finlay McWalterTalk 11:12, 21 November 2015 (UTC)[reply]
If you're only needing a small amount of information to be saved (less than 4096 bytes), then you can store it in a JavaScript cookie that you can create, write, read and destroy using the document.cookie interface (see: THIS for a good HOWTO document). You do need to add an expiry date to the cookie or the browser will delete it when you leave that web page - but you can set that date decades into the future and reset it each time the game is played - so it's not likely to be a problem.
The actual information will be squirreled away someplace by the browser, so it won't appear as an obvious file in the user's file system...and if you play the game on a different computer, you won't see your high score from the first session. For those reasons, storing the information in a database on the server is a much better way to go...but for a learning exercise in JavaScript, and if you don't want much more than the user's name, game preferences and a high score table - then you can certainly use a cookie. SteveBaker (talk) 13:51, 21 November 2015 (UTC)[reply]
Oh - you said you couldn't get to other sites (not even W3Schools?!?! http://www.w3schools.com -- YIKES!) - so I guess you'll need an actual example. I've added comments to the W3Schools example (I didn't test this - so I hope I didn't screw up - but it is pretty simple):

function setCookie(cname, cvalue, exdays)
{
  // cname is the name of the data (a text string),
  // cvalue is the value of that data (a text string)
  // exdays is the number of days until the cookie 'expires'
  //         (eg make exdays be 3652 for ~10 years!)

  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires="+d.toUTCString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}


function getCookie(cname)
{
  // cname is the name you gave to the date in 'setCookie'
  // the return value is the 'cvalue' you gave to 'setCookie'
  // it returns the empty string if you didn't create the cookie
  // yet (eg the first time you played the game).

  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++)
  {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1);
      if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
  }
  return "";
}

So when the program starts up you can use 'if (getCookie("highscore")=="") setCookie("highscore","0",3560) ;' - to check that the cookie exists and set the highscore to zero if it doesn't. Then at the end of each round of the game, 'var myHighScore = parseInt(getCookie("highscore"));' to fetch the previous high score and 'if (myCurrentScore > myHighScore) setCookie("highscore", myCurrentScore, 3560);' at the end of each round to save it.
SteveBaker (talk) 14:09, 21 November 2015 (UTC)[reply]

The ″cookie″ option is a good alternative. Thanks for helping me out. Bejacobs (talk) 18:22, 21 November 2015 (UTC)[reply]

C language to machine code

[edit]

Couldn't detect answer from Google... what langauge stands between C to machine code?... In Windows Os or Linux Os? is it one of the assembly languages? Or anything else? if at all? 176.12.141.2 (talk) 19:30, 21 November 2015 (UTC)[reply]

It depends on the compiler - the intermediate language article lists some. -- Finlay McWalterTalk 19:36, 21 November 2015 (UTC)[reply]
It also depends on the question. From a user-level, C is typically translated to machine code. There almost always is an intermediate step to assembly language, which is just a mnemonic form of machine language - there is a 1-to-1 correspondence between assembler instructions and machine instructions. As Finlay wrote, most modern compilers have a front-end translating to an intermediate representation, which then can be transformed and optimised before it's finally translated to the machine-specific language. But this intermediate language is usually neither directly manipulated by humans nor directly executed by a machine. --Stephan Schulz (talk) 20:44, 21 November 2015 (UTC)[reply]
"...there is a 1-to-1 correspondence between assembler instructions and machine instructions." That's frequently not the case. Many (most?) modern assemblers have all kinds of bells and whistles like macros, optimization, data typing, and even things like support for object-oriented programming (see assembly language). Of course no one is (usually) forcing you to use these features, but you can't assume by default that each assembler instruction maps to a single ISA instruction. --71.119.131.184 (talk) 05:42, 22 November 2015 (UTC)[reply]
It depends on how you define "instruction": There should be a 1-to-1 correspondence between the actual opcode mnemonics and the opcodes themselves (at least, this has been the case for every processor I've worked with). The other "bells and whistles" like data typing and macros use pseudo-instructions that are only meaningful to the assembler. OldTimeNESter (talk) 21:19, 23 November 2015 (UTC)[reply]
Using the GNU C compiler, you get the choice. If you compile using "gcc -o foo foo.c" then 'foo' will be in a binary machine code. But if you compile using "gcc -S foo.c" then you'll get a file called 'foo.s' which in assembly code. But if you grab a copy of Emscripten then you can compile C code into JavaScript!
None of that has anything to do with operating systems by the way - I can do all of those things with GNU C under Windows, Linux or MacOS.
In practice, under most operating systems, you go straight from C to machine code - just because going to assembly code and then assembling that in to machine code is a pointless waste of time. However, I have on occasion had reason to believe there was a bug in the compiler - and generating human-readable assembly code was an invaluable way to figure out what was going wrong. Sometimes, it's useful to compile very short C subroutines to Assembly language in order that you can hand-optimize them when the compiler isn't doing a good job of it...although I haven't had a need to do that for about 20 years - because compilers generally write better assembly code than I do.
There are some notable exceptions to that - I often need assembly-coded inserts into C++ programs when running on a microcontroller like Arduino because some low level hardware operations can't be written in C or C++. In such cases, I write a nice 'wrapper' function in C++, compile it with the '-S' option to get the assembly code, then hand edit it to put in a couple of lines of assembly code to do something very specific (eg turning interrupts on and off).
SteveBaker (talk) 21:05, 23 November 2015 (UTC)[reply]

My phone contacts disappeared even though I had never synced them with iCloud. Is there any way to get them back?"

[edit]

I have an iCloud account but I never use it. I wanted to eliminate it from my iPhone. But I didn't remember my password, so I tried to turn off the option o syncing my phone contacts with the iCloud account. But I have never synced it. So I turned it off. It showed a message saying that all my contacts that had been synced with iCloud would be eliminated. And all my phone contacts disappeared EVEN THOUGH I had never synced them with iCloud. Is there any way to get all those contacts back?

Posted on behalf of a user without www access by μηδείς (talk) 23:13, 21 November 2015 (UTC)[reply]

Sometime when you plug an iPhone into a PC with iTunes, it will back up the contents. This could be restored if it existed. Graeme Bartlett (talk) 09:51, 23 November 2015 (UTC)[reply]
Thanks, my friend actually thought of that solution immediately, but the phone involved belonged to someone else. At this point I've been asked to close the thread, with thanks, as hopelessly as academic. μηδείς (talk) 20:09, 23 November 2015 (UTC)[reply]
Resolved