www.webdeveloper.com
Results 1 to 8 of 8

Thread: please help me read a text filles length.

  1. #1
    Join Date
    Dec 2010
    Posts
    14

    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
    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();
        }
    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 + "");
                                                                   
    }
    However there is no output with this code: Say("Yes " + str + "");

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

    Thanks!

  2. #2
    Join Date
    Dec 2010
    Posts
    14
    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");			
    		}
    }
    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();
    }
    Edit I tried .size and .length for Length1 and 2... Any ideas?
    Last edited by magace; 06-03-2013 at 01:40 PM.

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,435

    Lightbulb

    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).

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <meta charset="utf-8">
    <script type="text/javascript">
    // From: 
    // http://www.webdeveloper.com/forum/showthread.php?278913-please-help-me-read-a-text-filles-length.&p=1270777#post1270777
    
    // Simulated 'yes.txt' file contents, 
    // read in via AJAX or PHP routine (obtained from somewhere)
    var txtYes = 'yes\nYes\nYES\n';
    
    // Simulated 'no.txt' file contents, 
    // read in via AJAX or PHP routine (obtained from somewhere)
    var txtNo  = 'no\nNo\nNO\n';
    
    // 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 = 'yes\nno\nYes\nNo\nYES\nNO\n';
    
    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);
      }
    }
    </script>
    
    </head>
    <body>
    <button onclick="count('yes')">Count 'Yes' votes</button>
    <button onclick="count('no')">Count 'No' votes</button>
    <button onclick="count('')">Count 'Yes/No' votes</button>
    
    </body>
    </html>

  4. #4
    Join Date
    Dec 2010
    Posts
    14
    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();
        }
    This still doesn't help me read all the data in the text file.
    Code:
    var txtYes = 'yes\nYes\nYES\n';
    var txtNo  = 'no\nNo\nNO\n';
    var txtYesNo = 'yes\nno\nYes\nNo\nYES\nNO\n';
    
    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);
      }
    }
    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();
    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?

  5. #5
    Join Date
    Dec 2010
    Posts
    14
    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?

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,435
    The ReadLine function may be sopported in the API program, but that doesn't mean
    it is supported by the JS of your browser.

  7. #7
    Join Date
    Dec 2010
    Posts
    14
    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();
             }
    Last edited by magace; 06-05-2013 at 03:27 PM.

  8. #8
    Join Date
    Dec 2010
    Posts
    14
    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");
             }
             }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles