| |

VerySource

 Forgot password?
 Register
Search
View: 888|Reply: 3

ifstream reads a 5m file with read (*), eof without reading

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-22 11:30:01
| Show all posts |Read mode
I used ifstream to read a 5m BMP file and read 4 bytes at a time, but when I read 3712 × 4 bytes, it was eof,
Stop there every time. I do not know why.
Ask for help


///////////////////////////////////////////////////////////////// /// //////////////////////////////////////
// Generate a bmp difference graphic file. In this file, all points that have changes are white: FF FF FF
// The points that have not changed are all black: 00 00 00
// Generate another difference file. That is, for each point, use the S8 mode and retain the difference
//
///////////////////////////////////////////////////////// //////////////////////////////////////////////
void MakeDisFile (char * filepath, int filenum) // filenum: number of files, file name, corresponding to 1. bmp ~ filenum. bmp
{
int headlength = 54;
char BMPfilename0 [100], BMPfilename1 [100], Bisfilename [100], Disfilename [100];
char FileHeader [54];
unsigned char FF [] = {255,255,255,255};
unsigned char OO [] = {0,0,0,0};
int sizePixel;
int countpixel;
typedef unsigned char Pixel [4];
Pixel pixel0, pixel1, Dispixel;
for (int i = 1; i <= filenum; i ++)
{
sprintf (BMPfilename0, "% s\\% d.bmp", filepath, i); // The current file
sprintf (BMPfilename1, "% s\\% d.bmp", filepath, i); // next file
sprintf (Bisfilename, "% s\\Bis% d.bmp", filepath, i); // Bisfilename: difference graphic file
sprintf (Disfilename, "% s\\Dis% d.dat", filepath, i); // Disfilename: difference value file

ifstream ifr0 (BMPfilename0);
ifstream ifr1 (BMPfilename1);
ofstream ofrBMP (Bisfilename);
ofstream ofrDis (Disfilename);
ifr0.read (FileHeader, headlength); // read out the file header, and then write to the Bisfilename
ofrBMP.write (FileHeader, headlength);

ifr1.seekg (headlength);

countpixel = 0;
Ranch
sizePixel = FileHeader [28] / 8; // bitperpixel / 8 = byteperpixel
while (! ifr1.bad ())
{
countpixel ++;
ifr0.read ((char *) pixel0, sizePixel);
ifr1.read ((char *) pixel1, sizePixel);
int dis = 0;
for (int tem_i = 0; tem_i <sizePixel; tem_i ++)
{
Dispixel [tem_i] = pixel0 [tem_i] -pixel1 [tem_i]; // for the distance
dis + = Dispixel [tem_i]; // dis accumulate the entire dis value, when 0: no change
}
// Dispixel difference written to file Disfile
ofrDis.write ((const char *) Dispixel, sizePixel);
Ranch
// If Dispixel is 000, indicating that this point has not changed, write Bisfile to 00 00 00
// if the dispixel is not 000 then write 'FF FF FF'to Bisfile
if (dis)
{// As long as one is non-zero, it means there is a change
ofrBMP.write ((const char *) FF, sizePixel);
}
else // no change
{
ofrBMP.write ((const char *) OO, sizePixel);
}
if (ifr0.eof ())
{
int temp;
temp = countpixel;
}
}
ifr0.close ();
ifr1.close ();
ofrBMP.close ();
ofrDis.close ();
    }
}
Reply

Use magic Report

0

Threads

14

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-5-4 09:45:01
| Show all posts
ifstream ifr0 (BMPfilename0, ios_base :: in | ios_base :: binary);
ifstream ifr1 (BMPfilename1, ios_base :: in | ios_base :: binary);
ofstream ofrBMP (Bisfilename, ios_base :: out | ios_base :: trunc | ios_base :: binary);
ofstream ofrDis (Disfilename, ios_base :: out | ios_base :: trunc | ios_base :: binary);
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-5-4 11:15:01
| Show all posts
First, you must use bin mode to open the file.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-6-3 13:00:01
| Show all posts
ou a~~~~ must use the binary method. . .
I later opened with fopen() b mode,
That's it.

Okay, thank you~~~
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list