By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

Reading a data file in JavaScript

P: n/a
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 = fso.OpenTextFile( Server.MapPath("orbit.csv"), ForReading);
return(f.ReadAll());
}

The data file has four columns and the first line maybe something like

1.23,3.45,6.78,2.34

I need access to the second and third column only. Can I use teh above
function - if so how exactly?

Thanks

Hardy
Jan 6 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
HardySpicer wrote:
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 = fso.OpenTextFile( Server.MapPath("orbit.csv"), ForReading);
return(f.ReadAll());
}

The data file has four columns and the first line maybe something like

1.23,3.45,6.78,2.34

I need access to the second and third column only. Can I use teh above
function - if so how exactly?
RTFM:

http://msdn2.microsoft.com/en-us/lib...4s(VS.85).aspx
http://msdn2.microsoft.com/en-us/library/312a5kbt.aspx
http://msdn2.microsoft.com/en-us/lib...dd(VS.85).aspx
http://msdn2.microsoft.com/en-us/lib...6b(VS.85).aspx
http://msdn2.microsoft.com/en-us/lib...fe(VS.85).aspx
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jan 6 '08 #2

P: n/a
On Jan 6, 2:33*pm, HardySpicer <gyansor...@gmail.comwrote:
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 = fso.OpenTextFile( Server.MapPath("orbit.csv"), ForReading);
* return(f.ReadAll());

}

The data file has four columns and the first line maybe something like

1.23,3.45,6.78,2.34

I need access to the second and third column only. Can I use teh above
function - if so how exactly?

Thanks

Hardy
Do you have some need to do this in JavaScript? Could you elaborate a
little more in what context you will be doing this?

Local file access form a browser is typically a no-no. ActiveX will
only work in IE, and at a minimum the user would be prompted to allow
or deny active x access to the file system.

And I have no idea what the "Server" object referenced here is
'Server.MapPath("orbit.csv")'

As far as accessing the 2nd and third columns, there are many ways
this can be done. You could read the whole file line by line, put each
line into a string, and parse the string using various string
functions, keying on the commas. Since it is a csv file, it can be
probably read in as a dataset, or recordset..oledb comes to mind. At
any rate, I would not use JavaScript for this.
Jan 6 '08 #3

P: n/a
On Jan 7, 10:31 am, Doug Gunnoe <douggun...@gmail.comwrote:
On Jan 6, 2:33 pm, HardySpicer <gyansor...@gmail.comwrote:
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 = fso.OpenTextFile( Server.MapPath("orbit.csv"), ForReading);
return(f.ReadAll());
}
The data file has four columns and the first line maybe something like
1.23,3.45,6.78,2.34
I need access to the second and third column only. Can I use teh above
function - if so how exactly?
Thanks
Hardy

Do you have some need to do this in JavaScript? Could you elaborate a
little more in what context you will be doing this?

Local file access form a browser is typically a no-no. ActiveX will
only work in IE, and at a minimum the user would be prompted to allow
or deny active x access to the file system.

And I have no idea what the "Server" object referenced here is
'Server.MapPath("orbit.csv")'

As far as accessing the 2nd and third columns, there are many ways
this can be done. You could read the whole file line by line, put each
line into a string, and parse the string using various string
functions, keying on the commas. Since it is a csv file, it can be
probably read in as a dataset, or recordset..oledb comes to mind. At
any rate, I would not use JavaScript for this.
Ok I need to plot the GPS orbit of a satellite. I have the data and I
want to use Google Maps. I know how to plot a line in Google Maps no
problem but I need to read the orbit data line by line then plot teh
data. Of course I can do this locally but I want others to get access
and at present the data is only simulated - it will be real at some
point.

I was also unsure how to access the data file: Microsoft gives this

a = fs.OpenTextFile("c:\\testfile.txt", ForAppending, false);

but what does this mean on a server? (ie the path is ??) The data is
on the server and not on my pc. Not worried about acknowledging active-
x btw - that's ok.

Hardy
Jan 6 '08 #4

P: n/a
On Jan 6, 3:48*pm, HardySpicer <gyansor...@gmail.comwrote:
Ok I need to plot the GPS orbit of a satellite. I have the data and I
want to use Google Maps. I know how to plot a line in Google Maps no
problem but I need to read the orbit data line by line then plot teh
data. Of course I can do this locally but I want others to get access
and at present the data is only simulated - it will be real at some
point.

I was also unsure how to access the data file: Microsoft gives this

a = fs.OpenTextFile("c:\\testfile.txt", ForAppending, false);

but what does this mean on a server? (ie the path is ??) The data is
on the server and not on my pc. Not worried about acknowledging active-
x btw - that's ok.

Hardy- Hide quoted text -

- Show quoted text -
Since the file is on a server, an activeX file system object will not
work. It would just try to access the user's system.

You could use a server side script to read the file and parse the
values you need. I know PHP has functions to parse and use CSV files.
This would be an easier way to handle the problem, IMO.

http://us.php.net/fgetcsv

And I'm sure other server side technologies do as well.

You can also use AJAX

http://developer.mozilla.org/en/docs...etting_Started

Jan 6 '08 #5

P: n/a
Doug Gunnoe wrote:
On Jan 6, 3:48 pm, HardySpicer <gyansor...@gmail.comwrote:
>Ok I need to plot the GPS orbit of a satellite. I have the data and I
want to use Google Maps. I know how to plot a line in Google Maps no
problem but I need to read the orbit data line by line then plot teh
data. Of course I can do this locally but I want others to get access
and at present the data is only simulated - it will be real at some
point.

I was also unsure how to access the data file: Microsoft gives this

a = fs.OpenTextFile("c:\\testfile.txt", ForAppending, false);

but what does this mean on a server? (ie the path is ??) The data is
on the server and not on my pc. Not worried about acknowledging active-
x btw - that's ok.
[...]
If you Google idio^W users would ever learn how to quote properly?
Since the file is on a server, an activeX file system object will not
work.
Yes, it will.
It would just try to access the user's system.
It will allow access to the filesystem of the computer it is being created on.

http://msdn2.microsoft.com/en-us/lib...eh(VS.85).aspx
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jan 6 '08 #6

P: n/a
On Jan 6, 4:24*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Yes, it will.
From client side javascript?
Jan 6 '08 #7

P: n/a
Doug Gunnoe wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
>Yes, it will.
By trimming too much, you have been destroying the context of the quoted
text here. Will you ever learn how to quote properly?
From client side javascript?
Nobody aside from you ever stated a restriction of a solution to client-side
JavaScript, let alone JavaScript instead of JScript. In fact, the OP
specifically asked about server-side JScript in
<8e**********************************@r60g2000hsc. googlegroups.com>.

You should read more thoroughly and be slower to jump to conclusions; that
would avoid such misunderstandings.
PointedEars
Jan 6 '08 #8

P: n/a
On Jan 6, 5:42*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>*Will you ever learn
Probably not.
Nobody aside from you ever stated a restriction of a solution to client-side
JavaScript,
Well if that is the case, all apologies to the OP.

(But I would still consider using something other than J\JavaScript
and a file system object for this.)
Jan 7 '08 #9

P: n/a
On Jan 7, 11:18 am, Doug Gunnoe <douggun...@gmail.comwrote:
On Jan 6, 3:48 pm, HardySpicer <gyansor...@gmail.comwrote:
Ok I need to plot the GPS orbit of a satellite. I have the data and I
want to use Google Maps. I know how to plot a line in Google Maps no
problem but I need to read the orbit data line by line then plot teh
data. Of course I can do this locally but I want others to get access
and at present the data is only simulated - it will be real at some
point.
I was also unsure how to access the data file: Microsoft gives this
a = fs.OpenTextFile("c:\\testfile.txt", ForAppending, false);
but what does this mean on a server? (ie the path is ??) The data is
on the server and not on my pc. Not worried about acknowledging active-
x btw - that's ok.
Hardy- Hide quoted text -
- Show quoted text -

Since the file is on a server, an activeX file system object will not
work. It would just try to access the user's system.

You could use a server side script to read the file and parse the
values you need. I know PHP has functions to parse and use CSV files.
This would be an easier way to handle the problem, IMO.

http://us.php.net/fgetcsv

And I'm sure other server side technologies do as well.

You can also use AJAX

http://developer.mozilla.org/en/docs...etting_Started
Yes I solved the problem in PHP but the map wasn't good enough -I
needed a zoom facility.
I then had the problem of how to combine PHP with Javascript (since
Javascript is used for Google Maps). I am not sure how to do this. I
need to read a line of data then plot. So I need PHP followed by
Javascript then repeat.

Hardy
Jan 7 '08 #10

P: n/a
On Jan 6, 11:47*pm, HardySpicer <gyansor...@gmail.comwrote:
Yes I solved the problem in PHP but the map wasn't good enough -I
needed a zoom facility.
I then had the problem of how to combine PHP with Javascript (since
Javascript is used for Google Maps). I am not sure how to do this. I
need to read a line of data then plot. So I need PHP followed by
Javascript then repeat.

Hardy- Hide quoted text -

- Show quoted text -
If you need to read the file - plot, read the file - plot because the
file is changing after the browser has loaded the page, then something
like AJAX would be a good choice, and plus it uses JavaScript like
Google maps (which is also an AJAX app I think).

But if the orbit.csv file does not change whilst the user has loaded
your page, then PHP can work with google maps, simply by writing
JavaScript vars with php.

For example, and consider this more or less pseudocode, and this can
be done many different ways

<script>
function plotStuff(){
var plotData = [];
for(var i = 0;i<something;i++){ //or some appropriate loop here
plotData[i] = <?php echo some_PHP_function('read the data') ?>;
}

</script>

where some_PHP_function would read the data you want to read.

Of course, there are many different ways to do this, you could read
everything into an array in PHP first, and then for each item in the
array do

plotData[i] = <?php echo plotDataPHP[i] ?>
And again, this is more or less pseudocode, so don't take it
literally, but the general idea will work.

After this, you have your info into javaScript and can do as you
please with it.
Jan 7 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.