0

How to read a binary files and print 3's on screen?

Hello, I want to read a binary file of integer type and print the occurrence of the number of 3's in the file. I somehow wrote a program to open and read a binary file. Here is the couple of problems I am facing: 1. If I try to print the file on my terminal, the execution continues forever and the loop never ends. 2. I have no idea of how to filter out 3's from it. Here is my code: #include <iostream> #include <fstream> using namespace std; int main () { streampos size; char * memblock; ifstream file ("threesData.bin", ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [size]; file.seekg (0, ios::beg); file.read (memblock, size); file.close(); cout << "the entire file content is in memory"; for (int i = 0; i < size; i += sizeof(int)) { cout << *(int*)&memblock[i] << endl; } delete[] memblock; } else cout << "Unable to open file"; return 0; }

24th Feb 2017, 9:56 PM
Safal Basnet
Safal Basnet - avatar
5 Antworten
+ 3
#include <iostream> #include <fstream> using namespace std; int main () { char Char; int count=0; ifstream file ("threesData.bin", ios::in|ios::binary|ios::ate); if(file.is_open()) { file.tellg(0,ios::beg); while(!file.eof()) { file>>Char; if(Char==3) count++; } cout<<count; } file.close(); return 0; }
25th Feb 2017, 3:54 AM
Megatron
Megatron - avatar
+ 3
file>>char; //reads a character from the stream file is similar to cin>>char; //reads a character from console(user) since file is similar to cin & cout while(!file.eof()) {file>>char; cout<<char;}
25th Feb 2017, 6:05 AM
Megatron
Megatron - avatar
+ 2
Well I know that there might be errors I never get a program right in my first try. But I am unaware of the way code blocks works and the way it refers to errors. But I will try to look up something if possible.
25th Feb 2017, 12:53 PM
Megatron
Megatron - avatar
0
Thanks a lot. Could you describe me the line file>>char. what actually is this code doing? Also, what if I were to print the complete file and not count 3?
25th Feb 2017, 5:40 AM
Safal Basnet
Safal Basnet - avatar
0
Your code is not compiling and the error is in tellg() member function. It says something like ||=== Build file: "no target" in "no project" (compiler: unknown) ===| C:\Users\safal\Desktop\CIS\CIS 6\Count3s.cpp\main.cpp||In function 'int main()':| C:\Users\safal\Desktop\CIS\CIS 6\Count3s.cpp\main.cpp|13|error: no matching function for call to 'std::basic_ifstream<char>::tellg(int, const seekdir&)'| C:\Users\safal\Desktop\CIS\CIS 6\Count3s.cpp\main.cpp|13|note: candidate is:| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\istream|571|note: std::basic_istream<_CharT, _Traits>::pos_type std::basic_istream<_CharT, _Traits>::tellg() [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::pos_type = std::fpos<int>]| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\istream|571|note: candidate expects 0 arguments, 2 provided| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
25th Feb 2017, 9:20 AM
Safal Basnet
Safal Basnet - avatar