How to read a local text file in the browser?

You need to check for status 0 (as when loading files locally with XMLHttpRequest, you don’t get a status returned because it’s not from a Webserver) function readTextFile(file) { var rawFile = new XMLHttpRequest(); rawFile.open(“GET”, file, false); rawFile.onreadystatechange = function () { if(rawFile.readyState === 4) { if(rawFile.status === 200 || rawFile.status == 0) { var … Read more

Clearing using jQuery

Easy: you wrap a <form> around the element, call reset on the form, then remove the form using .unwrap(). Unlike the .clone() solutions otherwise in this thread, you end up with the same element at the end (including custom properties that were set on it). Tested and working in Opera, Firefox, Safari, Chrome and IE6+. … Read more

Read whole ASCII file into C++ std::string [duplicate]

There are a couple of possibilities. One I like uses a stringstream as a go-between: std::ifstream t(“file.txt”); std::stringstream buffer; buffer << t.rdbuf(); Now the contents of “file.txt” are available in a string as buffer.str(). Another possibility (though I certainly don’t like it as well) is much more like your original: std::ifstream t(“file.txt”); t.seekg(0, std::ios::end); size_t … Read more

Read a file one line at a time in node.js?

Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here’s the easiest way to read lines from a file, without any external modules: const fs = require(‘fs’); const readline = require(‘readline’); async function processLineByLine() { const fileStream = fs.createReadStream(‘input.txt’); const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity }); // … Read more

Batch file to delete files older than N days

Enjoy: forfiles -p “C:\what\ever” -s -m *.* -d <number of days> -c “cmd /c del @path” See forfiles documentation for more details. For more goodies, refer to An A-Z Index of the Windows XP command line. If you don’t have forfiles installed on your machine, copy it from any Windows Server 2003 to your Windows XP machine at %WinDir%\system32\. … Read more

Read file line by line using ifstream in C++

First, make an ifstream: #include <fstream> std::ifstream infile(“thefile.txt”); The two standard methods are: Assume that every line consists of two numbers and read token by token: int a, b; while (infile >> a >> b) { // process pair (a,b) } Line-based parsing, using string streams: #include <sstream> #include <string> std::string line; while (std::getline(infile, line)) … Read more

How to append text to an existing file in Java?

Are you doing this for logging purposes? If so there are several libraries for this. Two of the most popular are Log4j and Logback. Java 7+ For a one-time task, the Files class makes this easy: try { Files.write(Paths.get(“myfile.txt”), “the text”.getBytes(), StandardOpenOption.APPEND); }catch (IOException e) { //exception handling left as an exercise for the reader … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)