/    Sign up×
Community /Pin to ProfileBookmark

please help me read a text filles length.

Ok im working on a little vote function that reads yes or no from separate .txt files
the votes are being saved line for line as followed:

[CODE]yes
yes
yes

or

no
no
no[/CODE]

Here is how I save the vote to text file:

[CODE] if(msg.indexOf(“yes”) != -1)
{
var fileNamess21 = “logs/webserver/normal/yeslog.txt”;
Files = FileOpen(fileNamess21, 2);
Files.WriteLine( “yes”)
Files.Close();
}

if(msg.indexOf( “no” ) != -1)
{
var fileNamess11 = “logs/webserver/normal/nolog.txt”;
Files = FileOpen(fileNamess11, 2);
Files.WriteLine( “no”)
Files.Close();
} [/CODE]

I need a function to read max length of yes and nolog.txt something like this:

[CODE]function checkvote()
{
Say(“Lets see who won.”);

var fileNamesss = “logs/webserver/normal/yeslog.txt”;
Filess = FileOpen(fileNamesss, 0)
if(Filess!=-1) // If the file has been successfully opened
{
length = flength(Filess);
str = fread(Filess, length);
Filess.Close();

}
Say(“Yes ” + str + “”);

}[/CODE]

However there is no output with this code: Say(“Yes ” + str + “”);

If anyone could help with the checkvote function it would be great!!

Thanks!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@magaceauthorJun 03.2013 — uhh ok so i tried this:

[CODE]function checkvote()
{
Say("Lets see who won.");
var YesFile = "logs/webserver/normal/yeslog.txt";
Length1 = YesFile.length
var NoFile = "logs/webserver/normal/nolog.txt";
Length2 = NoFile.length

if (Length1 == Length2)
{
Say("It looks like a tie...");
}
if (Length1 > Length2)
{
Say("It looks like yes won");
}
else
{
Say("It looks like no won");
}
}[/CODE]


However every time yes wins with a value of like 32 and no has a value of 31(even with 0 votes). Someone said I need to read the text file into a string then get the value of that. Any ideas?

Something like this? But it actualy needs to work :

[CODE]function checkvote()
{
Say("Lets see who won.");
var YesFile = "logs/webserver/normal/yeslog.txt";
File1 = FileOpen(YesFile, 1);
Rfile1 = File1.ReadAll();
Length1 = Rfile1.size

var NoFile = "logs/webserver/normal/nolog.txt";
File2 = FileOpen(NoFile, 1);
Rfile2 = File2.ReadAll();
Length2 = Rfile2.size

if (Length1 == Length2)
{
Say("It looks like a tie...");
}
if (Length1 > Length2)
{
Say("It looks like yes won");
}
else
{
Say("It looks like no won");
}
File1.Close();
File2.Close();
}[/CODE]


Edit I tried .size and .length for Length1 and 2... Any ideas?
Copy linkTweet thisAlerts:
@JMRKERJun 03.2013 — I'm not sure if you have one or two external files or if all responses are all in one file.

The following code assumes both, so eliminate whichever part you don't like.

Also, I'm assuming that the text files can be obtained with some PHP code or and AJAX function.

So I wrote this program with the text strings stimulated (see code).

<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;meta charset="utf-8"&gt;
&lt;script type="text/javascript"&gt;
// From:
// http://www.webdeveloper.com/forum/showthread.php?278913-please-help-me-read-a-text-filles-length.&amp;p=1270777#post1270777

// Simulated 'yes.txt' file contents,
// read in via AJAX or PHP routine (obtained from somewhere)
var txtYes = 'yesnYesnYESn';

// Simulated 'no.txt' file contents,
// read in via AJAX or PHP routine (obtained from somewhere)
var txtNo = 'nonNonNOn';

// Not exactly sure why two (2) text files are necessary
// as it could also be combined into one exteral text file
// and read in via AJAX or PHP routine (obtained from somewhere)
var txtYesNo = 'yesnnonYesnNonYESnNOn';

function count(info) {
var counter = []; var countYes = 0; countNo = 0;

if (info.toLowerCase() == 'yes') { // 'yes.txt' external file
counter = [];
counter = txtYes.split('n');
countYes = counter.length-1;
alert('Yes count: '+countYes);
}

if (info.toLowerCase() == 'no') { // 'no.txt' external file
counter = [];
counter = txtNo.split('n');
countNo = counter.length-1;
alert('No count: '+(counter.length-1));
}

if (info == '') { // combined yes/no external file
counter = [];
counter = txtYesNo.split('n');
countYes = 0; countNo =0;

<i> </i>for (var i=0; i&lt;counter.length-1; i++) {
<i> </i> if (counter[i].toLowerCase() == 'yes') { countYes++; }
<i> </i> if (counter[i].toLowerCase() == 'no') { countNo++; }
<i> </i>}
<i> </i>var msg = ''; if (countYes == countNo) { msg = 'nLooks like we have a tie'; }
// add your own message display logic for winners, losers and tiers.
alert('Yes count: '+countYes+'nNo count: '+countNo+msg);
}
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;button onclick="count('yes')"&gt;Count 'Yes' votes&lt;/button&gt;
&lt;button onclick="count('no')"&gt;Count 'No' votes&lt;/button&gt;
&lt;button onclick="count('')"&gt;Count 'Yes/No' votes&lt;/button&gt;

&lt;/body&gt;
&lt;/html&gt;

?
Copy linkTweet thisAlerts:
@magaceauthorJun 04.2013 — Hi thanks for replying I am actually using a client side not web! program based on javascript so I cant really use php and what not.

It does make more sense to use one text file or even a array I just went for what I "thought" would be the easiest.

Assuming I use this to save everything into one text file line for line.
[CODE] if(msg.indexOf("yes") != -1)
{
var fileNamess21 = "logs/webserver/normal/yesnolog.txt";
Files = FileOpen(fileNamess21, 2);
Files.WriteLine( "yes")
Files.Close();
}

if(msg.indexOf( "no" ) != -1)
{
var fileNamess11 = "logs/webserver/normal/yesnolog.txt";
Files = FileOpen(fileNamess11, 2);
Files.WriteLine( "no")
Files.Close();
}[/CODE]


This still doesn't help me read all the data in the text file.
[CODE]var txtYes = 'yesnYesnYESn';
var txtNo = 'nonNonNOn';
var txtYesNo = 'yesnnonYesnNonYESnNOn';

function count(info) {
var counter = []; var countYes = 0; countNo = 0;

if (info.toLowerCase() == 'yes') { // 'yes.txt' external file
counter = [];
counter = txtYes.split('n');
countYes = counter.length-1;
alert('Yes count: '+countYes);
}

if (info.toLowerCase() == 'no') { // 'no.txt' external file
counter = [];
counter = txtNo.split('n');
countNo = counter.length-1;
alert('No count: '+(counter.length-1));
}

if (info == '') { // combined yes/no external file
counter = [];
counter = txtYesNo.split('n');
countYes = 0; countNo =0;

for (var i=0; i<counter.length-1; i++) {
if (counter[i].toLowerCase() == 'yes') { countYes++; }
if (counter[i].toLowerCase() == 'no') { countNo++; }
}
var msg = ''; if (countYes == countNo) { msg = 'nLooks like we have a tie'; }
// add your own message display logic for winners, losers and tiers.
alert('Yes count: '+countYes+'nNo count: '+countNo+msg);
}
}[/CODE]


If I can write to a file this easy it has to be relatively easy to read it:
[CODE] var fileNamess21 = "logs/webserver/normal/yesnolog.txt";
Files = FileOpen(fileNamess21, 2);
Files.WriteLine( "yes")
Files.Close();[/CODE]


I do think it would be better to read from one file tho or even start using a array and just find size of yes or no array?
Copy linkTweet thisAlerts:
@magaceauthorJun 05.2013 — Sorry about double psot just wanted to say I know ReadLine is supported in the api for the program perhaps that can helps read the text file to get max number of lines?
Copy linkTweet thisAlerts:
@JMRKERJun 05.2013 — The ReadLine function may be sopported in the API program, but that doesn't mean

it is supported by the JS of your browser.
Copy linkTweet thisAlerts:
@magaceauthorJun 05.2013 — Thanks but like I said its a client side standalone program thats based on javascript. Not web related at all doesn't even use the browser. Here is what I tried doing using your method above and the readline however ofc it doesn't work ? I be noob.

It does look like readline is my best bet tho.

[CODE] Function CheckVote()
{
var countYes = 0;

var countNo = 0;
var yes = "yes";
var no = "no";
var YesFile = "logs/webserver/normal/yesnolog.txt";
File1 = FileOpen(YesFile, 0)
var neededLine = (File1.ReadLine())
while(!File1.eof) // While tempLog is open
{

if(neededLine.indexOf(yes) != -1) // If yes is found in log
{
countYes++;
}
if(neededLine.indexOf(no) != -1) // If no is found in log
{
countNo++;
}
}
if (countYes == countNo)
{
Say("We have a tie..");
}

if (countYes > countNo)
{
Say("Yes won");
}
else
{
Say("No won");
}
File1.Close();
}
[/CODE]
Copy linkTweet thisAlerts:
@magaceauthorJun 05.2013 — Muahaha I got it thanks for your help witht he counter and what not!

This seems to be working still got a few tweeks im sure but for now it works great!

[CODE]function checkvote()
{
var countYes = 0;

var countNo = 0;
var _yes = "yes"
var _no = "no"
var YesFile = "logs/webserver/normal/yesnolog.txt";
File1 = FileOpen(YesFile, 0)
Say("Lets see who won.");
while(!File1.eof) // While tempLog is open

{
var neededLine = (File1.ReadLine())
if(neededLine.indexOf(_yes) != -1) // If yes is found in log
{
countYes++;
Say("Counted a yes");
}
if(neededLine.indexOf(_no) != -1) // If no is found in log
{
countNo++;
Say("Counted a no");
}
}
if (countYes == countNo)
{
Say("We have a tie..");
}

if (countYes > countNo)
{
Say("Yes won");
}
else
{
Say("No won");
}
}[/CODE]
×

Success!

Help @magace spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...