# counting certain elements from a file

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-21-2005
greenstock
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 :D

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 :D

thanks! much apreciated, guys.
• 12-21-2005
Daved
What is your best attempt at an algorithm that might accomplish this?
• 12-21-2005
greenstock
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 :D ..i mean, to me handling arrays is a very easy thing :D 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 :D...i know i'm doing something wrong cuz i never get to looking at the next character from the file *worried*
• 12-21-2005
Daved
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'.
• 12-21-2005
greenstock
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 :confused:
• 12-21-2005
Dave_Sinkula
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 */```
• 12-21-2005
LuckY
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;  }```
• 12-22-2005
Stuka
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?
• 12-22-2005
spydoor
maybe, but it wouldn't help the original poster at all....
Quote:

oh and i have to do this without copying the information of the file into a variable...without using extra memory
• 12-22-2005
greenstock
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 ...
• 12-28-2005
greenstock
ok. i promissed so here is my code for this :D

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-28-2005
Daved
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.
• 12-28-2005
dwks
And main() returns an int.
• 01-01-2006
greenstock
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 :D 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 :D
• 01-01-2006
Daved
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last