Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to read cpp file like a txt file?
    text
    copied!<p>Im working on a web-based C++ IDE-like project. I wanted to add an open button in which the user can open not just a text file (done with that) but a .cpp file too. Will that be possible? </p> <p>Here is the code which opens .txt files and append to my contenteditable div. </p> <pre><code>function readSingleFile(evt) { var f = evt.target.files[0]; //console.log(f); if (!f) { alert("Failed to load file"); return; } if (f.name.indexOf('.txt') == -1) { alert(f.name + " is not a valid text file."); return; } var r = new FileReader(); r.onload = function (e) { var contents = e.target.result; //.replace("\r\n","&lt;br/&gt;"); contents = contents.replace(/&amp;/g, '&amp;amp;').replace(/&lt;/g, '&amp;lt;').replace(/&gt;/g, '&amp;gt;').replace(/"/g, '&amp;quot;'); alert("Got the file.n" + "name: " + f.name + "n" + "type: " + f.type + "n" + "size: " + f.size + " bytesn" + "contents: " + contents); var tmpSent = ""; var newContents = ""; for (var i = 0; i &lt; contents.length; i++) { if(contents.charAt(i) == '\n') { newContents += "&lt;div&gt;"+tmpSent+"&lt;/div&gt;"; tmpSent = ""; } else tmpSent += contents.charAt(i); }; if(tmpSent.length&gt;0) newContents += "&lt;div&gt;"+tmpSent+"&lt;/div&gt;"; console.log(newContents); document.getElementById('board').innerHTML = newContents; } r.readAsText(f); } document.getElementById('fileinput').addEventListener('change', readSingleFile, false); </code></pre> <p><a href="http://jsfiddle.net/88vtR/33/" rel="nofollow">http://jsfiddle.net/88vtR/33/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload