Q:
How to read and write a file using javascript?
Answer
Files can be read and written by using java script functions – fopen(),fread() and fwrite().
The function fopen() takes two parameters – 1. Path and 2. Mode (0 for reading and 3 for writing). The fopen() function returns -1, if the file is successfully opened.
Ex: file=fopen(getScriptPath(),0);
The function fread() is used for reading the file content.
Ex: str = fread(file,flength(file));
The function fwrite() is used to write the contents to the file.
Ex(): file = fopen("c:\MyFile.txt", 3);// opens the file for writing
fwrite(file, str);// str is the content that is to be written into the file.
View answer
Workspace
Report Error
Discuss