| |

VerySource

 Forgot password?
 Register
Search
View: 28|Reply: 11

Read the file below, why is the running result 1 2 3 4 5 6 7 7 an additional 7

[Copy link]

1

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-2-16 09:00:02
| Show all posts |Read mode
#include <fstream.h>
void main ()
{
ofstream fout ("d:\\intdata.dat");

int a [7] = {1,2,3,4,5,6,7};
for (int i = 0; i <7; i ++)
fout.write ((char *)&a [i], 4);
fout.close ();


ifstream fin ("d:\\intdata.dat");

int x;
 do {
fin.read ((char *)&x, 4);
cout << x << "";
} while (! fin.eof ());
fin.close ();

}
Reply

Use magic Report

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-4-17 00:30:01
| Show all posts
do {
fin.read ((char *)&x, 4);
cout << x << "";
} while (! fin.eof ());

You judge after reading first, if you write while in front, it won't be much
Reply

Use magic Report

1

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-4-17 13:00:02
| Show all posts
After the correction, the correct result cannot be obtained. . . . .
Reply

Use magic Report

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-4-17 18:00:01
| Show all posts
fin.read ((char *)&x, 4); // read one first, then

while (! fin.eof ()) {
cout << x << "";
fin.read ((char *)&x, 4);
}
Reply

Use magic Report

0

Threads

61

Posts

41

Credits

Newbie

Rank: 1

Credits
41

 China

Post time: 2020-4-18 08:45:01
| Show all posts
while (fin.read ((char *)&x, 4)&&! fin.eof ()) {
cout << x << "";
}
Reply

Use magic Report

0

Threads

61

Posts

41

Credits

Newbie

Rank: 1

Credits
41

 China

Post time: 2020-4-18 11:00:01
| Show all posts
After reading seven, it will not cause the eof () test to be "true". When reading the eighth, eof () is true, so there is an additional 7
Reply

Use magic Report

0

Threads

61

Posts

41

Credits

Newbie

Rank: 1

Credits
41

 China

Post time: 2020-4-18 15:15:01
| Show all posts
Or change it
while (! fin.eof ()&&fin.read ((char *)&x, 4)) {
cout << x << "";
}
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-5-20 22:00:01
| Show all posts
You open the file and take a look. If it is correct, your file ends with a space. I also encountered this problem. Press backspace and remove the space. That means the file should not be a space at the end, it should be 7. . The mouse cursor is next to 7
Reply

Use magic Report

1

Threads

21

Posts

15

Credits

Newbie

Rank: 1

Credits
15

 China

Post time: 2020-5-22 22:30:01
| Show all posts
int a [7] = {1,2,3,4,5,6,7};
for (int i = 0; i <7; i ++)
fout.write ((char *)&a [i], 4);

Write files 1,2,3,4,5,6,7
Then it is normal to read 1,2,3,4,5,6,7 when reading.
Reply

Use magic Report

1

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-6-2 11:45:01
| Show all posts
Please expert guidance. . . . . Still not working, why would I read it again
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list