U
Use*n*x
Hello,
I have a binary file (image file) and am reading 4-bytes at a time. The
File size is 63,480,320 bytes. My assumption is that if I loop through
this file reading 4 bytes at a time, I should loop 15,870,080 times.
The code is:
newprogram.cpp
=============
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int counter=0;
char * memblock;
memblock = new char [4];
ifstream file ("179060_mar_05_00_L7.024", ios::in|ios::binary);
file.seekg (1,ios::beg);
while (!file.eof())
{
file.read(memblock, 4);
counter++;
}
cout << "Number of loops: " << counter << "\n";
delete[] memblock;
file.close();
return 0;
}
$> g++ newprogram.cpp -o deletelater
$> ./deletelater
Number of loops: 15870080
(a) Notice the file.seekg (1,ios::beg); statement. Is this correct?
(b) If I were to use file.seekg (0,ios::beg); statement, the number of
loops would end up 15870081. Is file.seekg(0,ios::beg) correct? If so,
could you please help me understand why the loop goes 15870081 times?
If I were to use a variable: int tempdata;
and in the loop right after file.read, were to insert: tempdata = (int)
(*memblock), I would get different results with (a)
file.seekg(1,ios::beg) and (b) file.seekg(0,ios::beg). Which one would
be correct?
Your suggestions will be very helpful. Thank you.
Use*n*x
I have a binary file (image file) and am reading 4-bytes at a time. The
File size is 63,480,320 bytes. My assumption is that if I loop through
this file reading 4 bytes at a time, I should loop 15,870,080 times.
The code is:
newprogram.cpp
=============
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int counter=0;
char * memblock;
memblock = new char [4];
ifstream file ("179060_mar_05_00_L7.024", ios::in|ios::binary);
file.seekg (1,ios::beg);
while (!file.eof())
{
file.read(memblock, 4);
counter++;
}
cout << "Number of loops: " << counter << "\n";
delete[] memblock;
file.close();
return 0;
}
$> g++ newprogram.cpp -o deletelater
$> ./deletelater
Number of loops: 15870080
(a) Notice the file.seekg (1,ios::beg); statement. Is this correct?
(b) If I were to use file.seekg (0,ios::beg); statement, the number of
loops would end up 15870081. Is file.seekg(0,ios::beg) correct? If so,
could you please help me understand why the loop goes 15870081 times?
If I were to use a variable: int tempdata;
and in the loop right after file.read, were to insert: tempdata = (int)
(*memblock), I would get different results with (a)
file.seekg(1,ios::beg) and (b) file.seekg(0,ios::beg). Which one would
be correct?
Your suggestions will be very helpful. Thank you.
Use*n*x