22 Replies - 10982 Views - Last Post: 14 January 2010 - 11:02 AM
#1
Reading/Searching a file using a binary ifstream.
Posted 10 January 2010 - 01:41 AM
I want to do this with an ifstream. There are many different functions and I kinda know how to use them. I could use seekg and then use a loop but I don't know if that is the best way. Isn't there a way to get a set number of bytes at specific location? I used ifstream.read and specified 5 bytes but it always prints giberish to the console after it prints the 4 bytes.
Thanks.
Replies To: Reading/Searching a file using a binary ifstream.
#2
Re: Reading/Searching a file using a binary ifstream.
Posted 10 January 2010 - 02:23 AM
Even though it is not a terribly good example of what you want to do (sorry but I am too tiered to program effectively) you can look at this snippet which grabs a buffer of like 1024 chars (which it then displays in HEX)... Though that is probably not a great program to look at.
#3
Re: Reading/Searching a file using a binary ifstream.
Posted 10 January 2010 - 02:32 AM
#include <iostream>
#include <fstream>
const char PROGRAM_NAME[] = "read10Bytes.exe";
static const int Error_InvalidUsage = 1;
static const int Error_InvalidFile = 2;
int main(int argc, const char* argv[]) {
if (argc != 2) {
std::cout << "\nUsage: " << PROGRAM_NAME << " filename\n" << std::endl;
std::exit(Error_InvalidUsage);
}
std::fstream binfile(argv[1], std::ios::in | std::ios::binary | std::ios::out);
if (!binfile.is_open()) {
std::cout << "\nCould not open file: " << argv[1] << std::endl;
std::exit(Error_InvalidFile);
}
std::cout << "Moving to address 10 and reading 10 bytes...." << std::endl;
binfile.seekg(10);
char buffer[11];
binfile.read(buffer, 10);
buffer[10]=0; // adding terminating zero...
std::cout << "And the buffer held\"" << buffer << "\"" << std::endl;
binfile.close();
return 0;
}
I ran the program and asked it to look at its own source code file... the result was:
Moving to address 10 and reading 10 bytes.... And the buffer held"iostream> "
note that because there was a newline and the of the 10 bytes there is a newline in the output...
#4
Re: Reading/Searching a file using a binary ifstream.
Posted 10 January 2010 - 02:35 AM
NickDMax, on 10 Jan, 2010 - 03:23 AM, said:
Thanks Nick. I've been working on this and am getting closer. Is that why the code below prints giberish at the end? Isn't there a way to add the "\0" null at the end of the array?
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch;
char complete[4];
ifstream in("c:/test.txt", ios::in | ios::binary);
//in.seekg(10, ios::beg);
for(int i=0; i<=2; i++)
{
in.get(ch);
complete[i] = ch;
}
cout << complete;
return 0;
}
#5
Re: Reading/Searching a file using a binary ifstream.
Posted 10 January 2010 - 02:36 AM
Or you could tag it at the end of your loop, since the i variable is nice & fresh & still relevant
for(int i=0; i<=2; i++)
{
in.get(ch);
complete[i] = ch;
}
complete[i+1]='\0';
#6
Re: Reading/Searching a file using a binary ifstream.
Posted 10 January 2010 - 02:37 AM
#7
Re: Reading/Searching a file using a binary ifstream.
Posted 10 January 2010 - 03:20 AM
Is using a loop slower then using the ifstream.read function?
Thanks for the help. The simple things seem to get me confused.
#8
Re: Reading/Searching a file using a binary ifstream.
Posted 10 January 2010 - 11:01 PM
but... I can't seem to convert the input stream into an integer array for comparing pure binary data that is not in a text file. All I can do is input the stream into a character array. Do I have to use a built in function such as atoi() or will that cause conversion errors?
Thanks.
This post has been edited by Cyclone: 10 January 2010 - 11:06 PM
#9
Re: Reading/Searching a file using a binary ifstream.
Posted 11 January 2010 - 02:06 AM
#include <iostream>
#include <sstream>
using namespace std;
int main() {
char array[] = { '2', '3', ' ', '6', '\0' }; //a dummy array containing some numbers
stringstream sstr;
sstr<<array; //insert the array into the stringstream
int i, j; //we want to extract the numbers to these variables
sstr>>i>>j; //extract the first int element to i, second to j
cout<<i<<endl; //prints 23, which is the first integer element
cout<<j<<endl; //prints the second int element which is 6
return 0;
}
the default extraction operator separates elements by space character. The Above example shows basically how to convert from string to int.
Hope that helped you.
Edit: this way of converting requires you to have only integers in the array. So you have to make sure they are int and space separated.
This post has been edited by Anarion: 11 January 2010 - 02:13 AM
#10
Re: Reading/Searching a file using a binary ifstream.
Posted 11 January 2010 - 06:51 AM
reading a file in binary is great if you need to say read the bytes 0x08 0x09 in as 2312...
#11
Re: Reading/Searching a file using a binary ifstream.
Posted 11 January 2010 - 09:56 AM
NickDMax, on 11 Jan, 2010 - 04:21 PM, said:
reading a file in binary is great if you need to say read the bytes 0x08 0x09 in as 2312...
Yes Yes, this ability comes from the streams (same as filestream). I assumed converting from an array alone (not when reading them from a file stream)... my bad
This post has been edited by Anarion: 11 January 2010 - 09:57 AM
#12
Re: Reading/Searching a file using a binary ifstream.
Posted 11 January 2010 - 10:00 AM
char buffer[1024]; memset(buffer,'\0',1024);
This post has been edited by ImaSexy: 11 January 2010 - 10:01 AM
#13
Re: Reading/Searching a file using a binary ifstream.
Posted 14 January 2010 - 08:51 AM
out.write((char*)&n,byte_count);
I got the program to search\read\write bytes\text.
The last thing i'm trying to figure out is how to calculate the number of bytes a decimal number takes to write to the file.
For example:
decimal value of 255 takes up 1 byte(FF).
decimal value of 65535 takes up 2 bytes(FF FF).
and so on.
I have spent hours trying to figure out the math. It should be easy. I tried dividing by 255 and then counting how many times it takes to reach 255 etc. Even multiplying, dividing by base 16. If I use a calculator and divide a hex number by 255 I can get the correct results. But in c++ I don't know how to convert the number into hex so I can divide it by FF. If that makes sense.
#14
Re: Reading/Searching a file using a binary ifstream.
Posted 14 January 2010 - 09:11 AM
But really,
short int is 16bits (2 bytes)
int, long int are 32 bits (4 bytes)
long long is 64bits (8 bytes)
a 16 bit short pointer (not really used any more) is 2 bytes
a 32 bit far pointer is 4 bytes
a 64 bit far pointer (used on 64bit processors) is 8 bytes...
These are all pretty standard.
#15
Re: Reading/Searching a file using a binary ifstream.
Posted 14 January 2010 - 09:16 AM

New Topic/Question
Reply



MultiQuote




|