Thread: counting certain elements from a file

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    15

    counting certain elements from a file

    ok i know this might sound wierd, but the problem comes down to knowing how to do this ...and i just don't see how i could do it. maybe there's someone out there who can enlighten me

    ok the question is something like this (hope i'll be clear enough:-s ):

    consider a text file in which we have a binary number with a maximum of 1000000 digits. (that means i have a randome repetition of "1" and "0", no blanks). how do i count the number of zeros which follow the last "1"?
    example: my number 10011100011100000, and the number of zeros is 5. oh and i have to do this without copying the information of the file into a variable...without using extra memory

    thanks! much apreciated, guys.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What is your best attempt at an algorithm that might accomplish this?

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    15

    best attempt ahem...

    i don't know how to do this without using an array in which i've already copied the information from the text file ..i mean, to me handling arrays is a very easy thing but i get confused with files ...
    so here's the code...
    Code:
    #include <fstream.h>
    #include<conio.h>
    unsigned long  int nr;
    char a[1000000];
    void main(){
     ifstream f("nrbinar.in", ios::in);
         while(f.getline(a,1000000)){
                if (strcmp(a,"0")==0) nr+=nr;
                if(strcmp(a,"1")==0)  nr=0;  }
    
    
     cout<<nr;
     getch();
     }
    well i thought i'd reed the succsesive "0" and "1" s and when i found a "0" i would increment the counter... and so on ..and if i got a "1" after this succsession then i'd make the counter n=0 again because that succesion of zeros i've just counted wasn't formed out of the "zeros" i searched for ...i know i'm doing something wrong cuz i never get to looking at the next character from the file *worried*

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You've got the right idea for the counting. Try getting a single character at a time using operator >> or get() instead of a bunch of characters with getline. Then you can compare the character against '0' or '1'.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    15

    uf tried something else i think the counting's wrong

    Code:
    #include <fstream.h>
    #include<conio.h>
    unsigned long  int nr,n;
    char  a[1000000];
    
    void main(){
     fstream f("nrbinar.in", ios::in);
        f>>n;
        for(int i=0;i<n;i++){
             if(a[i]=='0') nr++;
             if(a[i]=='1')  nr=0;
                  }
    
     cout<<nr;
     getch();
    }
    i've added n as the number of digits of the binary number...to simplify things :S

    but for, lets say, a test file wherein the first line we have n and in the next line the n digit binary number
    19
    1000000010001111000

    what he "cout"s is 0 which means that he never gets to the next digit or i++ of the array

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think the gyst was more pointed to as being something more like this...
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
       std::ifstream file("file.txt");
       int ch, zeros = 0;
       while ( (ch = file.get()) != EOF )
       {
          if ( ch == '1' )
          {
             zeros = 0;
          }
          else if ( ch == '0' )
          {
             ++zeros;
          }
       }
       std::cout << "zeros = " << zeros << '\n';
       return 0;
    }
    
    /* file.txt
    10011100011100000
    */
    
    /* my output
    zeros = 5
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Just go to the end and read backwards until you encounter a 1. There's no reason to read any bytes left of that 1.
    Code:
    #include <fstream>
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main() {
      std::ifstream fin("bigbin.txt");
      if (!fin.is_open()) {
        cout << "Couldn't open the file" << endl;
        return 1;
      }
      
      fin.seekg(0, std::ios_base::end);
      long end = fin.tellg();
      
      unsigned count = 0;
      
      while (end > 0) {
        fin.seekg(--end, std::ios_base::beg);
        if (fin.peek() == '1')
          break;
        ++count;
      }
      
      cout << count << " zeros at the tail." << endl;
      
      return 0;  
    }

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    Call me lazy, but wouldn't reading the whole file (since it has no blanks) into a std::string, then using rfind to find the last 1, then do a single subraction to get the number be MUCH simpler and easier to understand/read?

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    maybe, but it wouldn't help the original poster at all....
    oh and i have to do this without copying the information of the file into a variable...without using extra memory

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    15
    Quote Originally Posted by spydoor
    maybe, but it wouldn't help the original poster at all....
    i knwo it wouldn't ... i've done it differently ...i'll give the code after i finish some stuff ..the actual problem i was workin on wanted a bit more than counting the ending zeros...that's just a strategy to help you figure out something else ...

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    15
    ok. i promissed so here is my code for this

    Code:
    #include <fstream.h>
    #include<conio.h>
    unsigned long  int nr=0;
    char  a;
    void main(){
    fstream f("nrbinar.in", ios::in);
     while(!f.eof()){
         f>>a;
         if(a=='1') nr=0;
         if(a=='0') nr++;}
    
    cout<<nr-1;
    getch();
    }
    it does what i wanted it to do, aka count the zeros from the end of a binary number from a file (namely "nrbinar.in").
    ....
    unfortunately this only works (for wht i need it to) only when i have at least one '1' in the file... for the number "0000000000", though, it's a bit tricky ...

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Well done. I assume that "0000000000" should output 0 since there are no '1's in the file. Otherwise, your code looks like it should output 10 which is the number of zeroes.

    To get it to output 0 if there is no '1' in the code, you will have to add a bit more logic. One possibility is to have a simple bool variable that starts off false and is set to true when a one is found. When the loop is done, if that variable is still false then there were no ones in the file and you can just set nr back to 0.

    Another note: you shouldn't have to subtract 1 from nr at the end. This will not work if the last digit in the file is a 1 (it will output -1 instead of 0). The reason you get an extra count under most inputs is that f.eof() only returns true after the entire file is read. A better way to loop would be to put the read into the while loop (e.g. while (f>>a) instead of while (!f.eof())). If you did that, the while loop would automatically stop when the read failed because of the end of the file.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And main() returns an int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    15
    thanks Daved. I was thinking about using another variable to see how <i>many</i> ones there were in the file but the mistake i kept doing was that i wanted to use that variable in the while which was plain wrong. *giggle*
    oh yeah and thanks for the while(f>>a)...i knew there must be something about the f.eof() thing, but i didn't bother myself with it, seeing i could just cout "nr-1"

    ok i've kept trying but i just think i'm aproaching this wrongly. well the thing is this works only for numbers that have minimum one zero at the end, namely straight numbers, and doesn't for an odd number, so... i firstly have to check whether it is a straight or an odd number ...any suggestions? i mean without having to read it all the way to the end that is

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It depends on your new code. In the last code posted, changing to while(f>>a) and getting rid of the nr-1 would have fixed the problem with running it on odd numbers. If you have made that fix but it is still not working, you'll want to post your new code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM