Search File

Search for character in file

Page 1 of 1

6 Replies - 748 Views - Last Post: 29 October 2010 - 09:59 AM Rate Topic: -----

#1 geofbot  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Search File

Posted 29 October 2010 - 08:20 AM

Hello everyone!

I have updated my "hash2binary" thing (remember that?), so that it will count how many 1s are in the binary. Here is what I have so far.
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
using namespace std;


int main() {
	int oneCount = 0;
	cout << "Hash:  ";
	string str;
	string temp;
	getline(cin, str);
	fstream file;
	file.open("file.txt");
	for(int i = 0; i < str.length(); i++)  {
		// cout << str[i] << ":  " << bitset<8>(str[i]) << "\n";
		cout << bitset<8>(str[i]);
		file << bitset<8>(str[i]);
	}
	for(int i = 0; i <= 8 * str.length(); i++)  {
		if()  {		// What to put in if?
			oneCount++;
		}
	}
	cout << "\n" << oneCount << " 1s out of " << 8 * str.length() << "\n";
	file.close();
	return 0;
}


As you can see in the comment, I have no idea how to check if the file character i if a 1 or not. Does anyone know how? (I am relatively new with outputting files)

GEOFBOT

Is This A Good Question/Topic? 1
  • +

Replies To: Search File

#2 geofbot  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Re: Search File

Posted 29 October 2010 - 09:25 AM

In my vain attempt to find the solution, I got this:
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
using namespace std;


int main() {
	int oneCount = 0;
	cout << "Hash:  ";
	string str;
	char *temp = 0;
	getline(cin, str);
	fstream file;
	file.open("file.txt");
	for(int i = 0; i < str.length(); i++)  {
		// cout << str[i] << ":  " << bitset<8>(str[i]) << "\n";
		cout << bitset<8>(str[i]);
		file << bitset<8>(str[i]);
	}
	for(int i = 0; i <= 8 * str.length(); i++)  {
		file.read(temp, 1);
		if(*temp == '1')  {
			oneCount++;
		}
	}
	cout << "\n" << oneCount << " 1s out of " << 8 * str.length() << "\n";
	file.close();
	return 0;
}

Now I'm getting this null pointer error! Bah! :pinch:
Was This Post Helpful? 1
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Search File

Posted 29 October 2010 - 09:34 AM

In the code below is one thing you should definitely add to your code.
Always check that any file stream you try to open has successfully opened before doing any work on it.

The other thing is a hint to how I would do what you want to do. Can you see how to take that hint further?
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
#include <sstream>
using namespace std;

int main() {
	int oneCount = 0;
	cout << "Hash:  ";
	string str;
	string temp;
	getline(cin, str);
	fstream file;
	file.open("file.txt");
	// ALWAYS CHECK YOUR FILE IS ACTUALLY OPEN
	if(!file.is_open()){
		cout << "file failed to open " << endl;
		return 1;
	}
	for(int i = 0; i < str.length(); i++)  {
		// cout << str[i] << ":  " << bitset<8>(str[i]) << "\n";
		// cout << bitset<8>(str[i]);
		file << bitset<8>(str[i]);
	}	
		
	stringstream strStm;	
	for(int i = 0; i < str.length(); i++)  {
		strStm << bitset<8>(str[i]);
	}
	string theString;
	strStm >> theString;
 	cout << "theString = " << theString << endl;
	
	// for(int i = 0; i <= 8 * str.length(); i++)  {
	// 	if()  {		// What to put in if?
	// 		oneCount++;
	// 	}
	// }
	cout << "\n" << oneCount << " 1s out of " << 8 * str.length() << "\n";
	file.close();
	return 0;
}


Was This Post Helpful? 2
  • +
  • -

#4 geofbot  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Re: Search File

Posted 29 October 2010 - 09:40 AM

Oops, I guess I was just being lazy. And now about the hint...let me think...

Oh, I get it! Aren't you dumping the 0s and 1s into the stringstream and then putting it into the string? Then I can search the string with array indexing or whatever! Let me see if that works...
Was This Post Helpful? 1
  • +
  • -

#5 geofbot  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Re: Search File

Posted 29 October 2010 - 09:49 AM

It worked! I didn't really need the fstream, it was just my crazy idea of using a file to hold the data. Here is my final code:
#include <iostream>
// #include <fstream>
#include <string>
#include <bitset>
#include <sstream>
using namespace std;


int main() {
	int oneCount = 0;
	cout << "Hash:  ";
	string str;
	string temp;
	stringstream strstrm;
	getline(cin, str);
// 	fstream file;
// 	file.open("file.txt");
// 	if(!file.is_open())  {
// 		cout << "Error in opening file!\n";
// 		return 1;
// 	}
	for(int i = 0; i < str.length(); i++)  {
//		cout << str[i] << ":  " << bitset<8>(str[i]) << "\n";
		cout << bitset<8>(str[i]);
// 		file << bitset<8>(str[i]);
		strstrm << bitset<8>(str[i]);
	}
	strstrm >> temp;
	for(int i = 0; i < temp.length(); i++)  {
		if(temp[i] == '1')  oneCount++;
	}
	cout << "\n" << oneCount << " 1s out of " << temp.length() << "\n";
// 	file.close();
	return 0;
}

Here is the console output (using a real hash):
Hash:  ee2173d208ebd9b50cafcfed60f5d5fb
01100101011001010011001000110001001101110011001101100100001100100011000000111000
01100101011000100110010000111001011000100011010100110000011000110110000101100110
01100011011001100110010101100100001101100011000001100110001101010110010000110101
0110011001100010
111 1s out of 256
Press any key to continue . . .

Note: the system pause was added automatically by vc++ (when running, not debugging). When running the program outside of vc++, there is no system pause.
Was This Post Helpful? 1
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Search File

Posted 29 October 2010 - 09:53 AM

Or you could just consume one char at a time out of the stringstream and check it as it comes out, keeping a count of the 1s.
Slightly more elegant than via the string step.
But the string way will work.
Was This Post Helpful? 1
  • +
  • -

#7 geofbot  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Re: Search File

Posted 29 October 2010 - 09:59 AM

Yay, you saved my code (once more)! Thanks for your help, janotte!

GEOFBOT
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1