473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Reading and/or writing a text file in javascript not working

5
Dear Sir,
Why this code can't be ran in my Internet Explorer?
Am I doing wrong in using ActiveXObjectScripting.FileSystemObject")?

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head><title>Reading/Writing Text File in Javascript</title>
  3. </HEAD>
  4. <body>
  5. <script  language="Javascript">
  6. var  fso  =  new  ActiveXObject("Scripting.FileSystemObject");  
  7. var  a  =  fso.CreateTextFile("c:\\testfile.txt",  true);  
  8. a.WriteLine("This  is  a  test.");  
  9. a.Close();  
  10. alert("testfile.txt has been created successfully");  
  11. </script>  
  12. </body></HTML>
[Please use CODE tags when posting source code. Thanks! --pbmods]
Jul 19 '07 #1
7 2090
pbmods
5,821 Expert 4TB
Heya, Fuji. Welcome to TSDN!

Try OpenTextFile().
Jul 19 '07 #2
Fuji
5
I've worked out. Thanks a lot!
Jul 20 '07 #3
Fuji
5
Dear Sir,
This code is executable but has wrong array's value.
Can anyone put array's value in the right place?
for example:
list[0,0] = Item001 list[0,0] = A001
list[1,0] = Item002 list[0,1] = B001
list[2,0] = Item003 list[0,2] = C001
list[3,0] = Item004 list[0,3] = D001
list[4,0] = Item005 list[0,4] = E001
stocklist.txt:
Item001|A001|99|A002|98
Item002|B001|97|B002|96
Item003|C001|95|C002|94
Item004|D001|93|D001|92
Item005|E001|91|E002|90

stocklist.htm:
Expand|Select|Wrap|Line Numbers
  1. 1.<html>
  2. 2.<head><title>JavaScript Example</title> 
  3. 3.</HEAD>
  4. 4.<script language=javascript>
  5. 5.var list = new Array();
  6. 6.var i = 0;
  7. 7.var j = 0;
  8. 8.function ReadFromFile(){
  9. 9.var sText = ""; 
  10. 10.var fso = new ActiveXObject("Scripting.FileSystemObject"); 
  11. 11.var FileObject = fso.OpenTextFile("C:\\stocklist.txt", 1); 
  12. 12.sText = FileObject.readall();
  13. 13.FileObject.close();
  14. 14.var CR = String.fromCharCode(13);
  15. 15.lineData = sText.split(CR);
  16. 16.var transdata  = "";
  17. 17.do
  18. 18.{address = lineData[i].split("|");  
  19. 19.  for (j=0; j<5; j++)
  20. 20.   {       
  21. 21.     transdata = address[j];
  22. 22.     list[i,j] = transdata;
  23. 23. }
  24. 24.     i=i+1;    
  25. 25.}while(i==5);
  26. 26.document.write(p[4,0]+p[4,1]+p[4,2]+p[4,3]+p[4,4]+",");
  27. 27.document.write(p[3,0]+p[3,1]+p[3,2]+p[3,3]+p[3,4]+",");
  28. 28.document.write(p[2,0]+p[2,1]+p[2,2]+p[2,3]+p[2,4]+",");
  29. 29.document.write(p[1,0]+p[1,1]+p[1,2]+p[1,3]+p[1,4]+",");
  30. 30.document.write(p[0,0]+p[0,1]+p[0,2]+p[0,3]+p[0,4]+",");
  31. 31.} 
  32. 32.</script>
  33. 33.<body>
  34. 34.<INPUT onclick=ReadFromFile() type=button value="ReadFromFile">  
  35. 35.</body></HTML>
  36.  
Any help will be very appreciated.
Jul 20 '07 #4
pbmods
5,821 Expert 4TB
Fuji, please use CODE tags when posting source code. See the REPLY GUIDELINES box on the right side of the page next time you post.
Jul 20 '07 #5
Fuji
5
[Thanks! Code Tags will be used next time. Does anyone know how to put correct value in array? Please see post code.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>JavaScript Example</title> 
  3. </HEAD>
  4. <script language=javascript>
  5. var list = new Array();
  6. var i = 0;
  7. var j = 0;
  8. function ReadFromFile(){
  9. var sText = ""; 
  10. var fso = new ActiveXObject("Scripting.FileSystemObject"); 
  11. var FileObject = fso.OpenTextFile("C:\\stocklist.txt", 1); 
  12. sText = FileObject.readall();
  13. FileObject.close();
  14. var CR = String.fromCharCode(13);
  15. lineData = sText.split(CR);
  16. var transdata  = "";
  17. do
  18. {address = lineData[i].split("|");  
  19.   for (j=0; j<5; j++)
  20.    {       
  21.      transdata = address[j];
  22.      list[i,j] = transdata;
  23.    }
  24.      i=i+1;    
  25. }while(i==5);
  26. document.write(list[4,0]+list[4,1]+list[4,2]+list[4,3]+list[4,4]+",");
  27. document.write(list[3,0]+list[3,1]+list[3,2]+list[3,3]+list[3,4]+",");
  28. document.write(list[2,0]+list[2,1]+list[2,2]+list[2,3]+list[2,4]+",");
  29. document.write(list[1,0]+list[1,1]+list[1,2]+list[1,3]+list[1,4]+",");
  30. document.write(list[0,0]+list[0,1]+list[0,2]+list[0,3]+list[0,4]+",");
  31. </script>
  32. <body>
  33. <INPUT onclick=ReadFromFile() type=button value="ReadFromFile">  
  34. </body></HTML>
  35.  
Jul 21 '07 #6
pbmods
5,821 Expert 4TB
Heya, Fuji.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Jul 21 '07 #7
Fuji
5
Dear Sir,

What do you want your code to do? Give an example.
In this example, I want this code to do that is just like this.
data2[0,0] = array00
data2[0,1] = array01
data2[0,2] = array02
data2[0,3] = array03

What is your code doing that you don't want it to do? Give an example.
data2[0,0] = array10
data2[0,1] = array11
data2[0,2] = array12
data2[0,3] = array13

What is your code *not* doing that it is supposed to? Give an example.
After runnung this code in IE, "document.write(data2[0,0] + data2[0,1] + data2[0,2] + data2[0,3] + data2[0,4]);" prints out the result is not supposed to have.
What I want is
data2[0,0] = array00
data2[0,1] = array01
data2[0,2] = array02
data2[0,3] = array03
What do not I want is
data2[0,0] = array10
data2[0,1] = array11
data2[0,2] = array12
data2[0,3] = array13

<html>
<head><title>JavaScript Example</title>
</HEAD>
<script language=javascript>
data1 = new Array();
data2 = new Array();
function javascriptarray(){
data1[0] = "array:"+0+0;
data1[1] = "array:"+0+1;
data1[2] = "array:"+0+2;
data1[3] = "array:"+0+3;
data2[0,0] = data1[0];
data2[0,1] = data1[1];
data2[0,2] = data1[2];
data2[0,3] = data1[3];

data1[0] = "array:"+1+0;
data1[1] = "array:"+1+1;
data1[2] = "array:"+1+2;
data1[3] = "array:"+1+3;
data2[1,0] = data1[0];
data2[1,1] = data1[1];
data2[1,2] = data1[2];
data2[1,3] = data1[3];

document.write(data2[0,0] + data2[0,1] + data2[0,2] + data2[0,3] + data2[0,4]);
}
</script>
<body>
<INPUT onclick=javascriptarray() type=button value="arraycheck">
</body></HTML>


Thanks a lot! Everyone.
Jul 24 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
2
by: Steve | last post by:
OK, I know I can't read a file with JavaScript, but I see references that I can use the filesystemobject which is part of vb script. Can I embed vb script in HTML? I am writing a program for...
9
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
3
by: Sivaraj G via .NET 247 | last post by:
We created a unicode file using java application. It usesmethods like writeUTF(), writeInt() of java.io.DataOutputStreamclass to write the content of the file. We are able to read datausing...
8
by: Raghu | last post by:
Is it possible to write a new file and after writing few bytes, is it possible to read from it from another file stream while write continues? Is there another steam for this type of operation? ...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
by: Clive Green | last post by:
Hello peeps, I am using PHP 5.2.2 together with MP3_Id (a PEAR module for reading and writing MP3 tags). I have been using PHP on the command line (Mac OS X Unix shell, to be precise), and am...
10
by: HardySpicer | last post by:
I have a cvs data file orbit.csv and found this code. function ReadFile() { var fso, f1, ts, s; var ForReading = 1; fso = new ActiveXObject("Scripting.FileSystemObject"); f =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.