Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mytests
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
songxinkai
mytests
Commits
fa95df52
Commit
fa95df52
authored
Oct 29, 2019
by
songxinkai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add binary_read_write_data
parent
ec1cb75e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
148 additions
and
100 deletions
+148
-100
c++/c++file/binary_read_write/main.cpp
+36
-100
c++/c++file/binary_read_write/main1.cpp
+112
-0
No files found.
c++/c++file/binary_read_write/main.cpp
View file @
fa95df52
// g++ main.cpp -std=c++11
#include <iostream>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using
namespace
std
;
using
namespace
std
;
void
read
(
const
string
file
,
vector
<
vector
<
float
>
>&
feas
,
int
main
(){
vector
<
vector
<
float
>
>&
pis
,
vector
<
float
>&
vs
){
ofstream
output
(
"test.bin"
,
ios
::
out
|
ios
::
binary
);
int
steps_in
=
0
;
int
count
=
400
;
int
winner_in
=
0
;
short
moves
[
1000
];
char
feas_in
[
1000
*
17
*
19
*
19
];
float
pis
[
1000
*
362
];
float
pis_in
[
1000
*
362
];
for
(
int
i
=
0
;
i
<
count
;
++
i
){
std
::
ifstream
input
(
file
,
ios
::
in
|
ios
::
binary
);
moves
[
i
]
=
i
;
input
.
read
((
char
*
)
&
steps_in
,
sizeof
(
int
));
for
(
int
j
=
0
;
j
<
362
;
++
j
){
// CHECK_LE(steps_in, 1000);
pis
[
i
*
362
+
j
]
=
float
(
i
+
j
*
0.001
);
input
.
read
((
char
*
)
&
winner_in
,
sizeof
(
int
));
}
input
.
read
((
char
*
)
feas_in
,
sizeof
(
char
)
*
19
*
19
*
17
*
steps_in
);
}
input
.
read
((
char
*
)
pis_in
,
sizeof
(
float
)
*
(
19
*
19
+
1
)
*
steps_in
);
cout
<<
"sizeof(count): "
<<
sizeof
(
count
)
<<
endl
;
input
.
close
();
cout
<<
"sizeof(moves): "
<<
sizeof
(
moves
)
<<
endl
;
feas
.
resize
(
steps_in
);
cout
<<
"sizeof(pis): "
<<
sizeof
(
pis
)
<<
endl
;
pis
.
resize
(
steps_in
);
output
.
write
((
char
*
)
&
count
,
sizeof
(
count
));
vs
.
resize
(
steps_in
);
output
.
write
((
char
*
)
moves
,
sizeof
(
short
)
*
count
);
for
(
int
step
=
0
;
step
<
steps_in
;
++
step
){
output
.
write
((
char
*
)
pis
,
sizeof
(
float
)
*
362
*
count
);
feas
[
step
].
resize
(
17
*
19
*
19
);
pis
[
step
].
resize
(
1
+
19
*
19
);
for
(
int
j
=
0
;
j
<
17
*
19
*
19
;
++
j
){
feas
[
step
][
j
]
=
feas_in
[
step
*
17
*
19
*
19
+
j
]
?
1.0
:
0.0
;
}
for
(
int
j
=
0
;
j
<
1
+
19
*
19
;
++
j
){
pis
[
step
][
j
]
=
pis_in
[
step
*
(
1
+
19
*
19
)
+
j
];
}
if
((
step
%
2
==
0
)
^
(
winner_in
==
1
)){
// self lose
vs
[
step
]
=
-
1.0
;
}
else
{
vs
[
step
]
=
1.0
;
}
}
}
void
write
(
const
string
file
){
int
steps_out
=
400
;
int
winner_out
=
1
;
// 1: black win, 0: white win
unsigned
char
*
feas_out
=
new
unsigned
char
[
steps_out
*
17
*
19
*
19
];
float
*
pis_out
=
new
float
[
steps_out
*
362
];
srand
((
int
)
time
(
0
));
for
(
int
i
=
0
;
i
<
steps_out
;
++
i
){
int
position_0
=
int
(
361.0
*
rand
()
/
(
RAND_MAX
+
1.0
));
int
position_1
=
int
(
361.0
*
rand
()
/
(
RAND_MAX
+
1.0
));
while
(
position_1
==
position_0
){
position_1
=
int
(
361.0
*
rand
()
/
(
RAND_MAX
+
1.0
));
}
for
(
int
c
=
0
;
c
<
16
;
++
c
){
feas_out
[
i
*
17
*
19
*
19
+
position_0
*
17
+
c
]
=
(
unsigned
char
)
1
;
}
for
(
int
c
=
0
;
c
<
2
;
++
c
){
feas_out
[
i
*
17
*
19
*
19
+
position_1
*
17
+
c
]
=
(
unsigned
char
)
1
;
}
pis_out
[
i
*
362
+
position_0
]
=
0.9
;
pis_out
[
i
*
362
+
position_1
]
=
0.1
;
for
(
int
j
=
0
;
j
<
19
*
19
;
++
j
){
feas_out
[
i
*
17
*
19
*
19
+
j
*
17
+
16
]
=
(
i
%
2
==
0
)
?
(
unsigned
char
)
1
:
(
unsigned
char
)
0
;
}
}
std
::
ofstream
output
(
file
,
ios
::
out
|
ios
::
binary
);
output
.
write
((
char
*
)
&
steps_out
,
sizeof
(
int
));
output
.
write
((
char
*
)
&
winner_out
,
sizeof
(
int
));
output
.
write
((
char
*
)
feas_out
,
sizeof
(
char
)
*
19
*
19
*
17
*
steps_out
);
output
.
write
((
char
*
)
pis_out
,
sizeof
(
float
)
*
(
19
*
19
+
1
)
*
steps_out
);
output
.
close
();
output
.
close
();
std
::
ofstream
finish_out
(
file
+
".finish"
,
ios
::
out
|
ios
::
binary
);
finish_out
.
close
();
}
int
main
(){
int
count_in
=
400
;
stringstream
ss
;
short
moves_in
[
1000
];
// for (int i = 0; i < 1000; ++i){
float
pis_in
[
1000
*
362
];
// ss.str("");
ifstream
input
(
"test.bin"
,
ios
::
in
|
ios
::
binary
);
// ss << "go/" << i << ".bin";
input
.
read
((
char
*
)
&
count_in
,
sizeof
(
count_in
));
// write(ss.str());
input
.
read
((
char
*
)
moves_in
,
sizeof
(
short
)
*
count_in
);
// }
input
.
read
((
char
*
)
pis_in
,
sizeof
(
float
)
*
362
*
count_in
);
vector
<
vector
<
float
>
>
feas
;
input
.
close
();
vector
<
vector
<
float
>
>
pis
;
cout
<<
count_in
<<
endl
;
vector
<
float
>
vs
;
for
(
int
i
=
0
;
i
<
count_in
;
++
i
){
read
(
"0.bin"
,
feas
,
pis
,
vs
);
cout
<<
moves_in
[
i
];
cout
<<
feas
.
size
()
<<
", "
<<
pis
.
size
()
<<
", "
<<
vs
.
size
()
<<
", "
<<
endl
;
if
(
i
==
count_in
-
1
)
cout
<<
endl
;
for
(
int
i
=
0
;
i
<
feas
.
size
();
++
i
){
else
cout
<<
", "
;
cout
<<
"========"
<<
i
<<
"======"
<<
endl
;
}
for
(
int
j
=
0
;
j
<
19
*
19
;
++
j
){
for
(
int
i
=
0
;
i
<
count_in
*
362
;
++
i
){
cout
<<
j
<<
": "
;
cout
<<
pis_in
[
i
];
for
(
int
c
=
0
;
c
<
17
;
++
c
){
if
((
i
+
1
)
%
362
==
0
){
cout
<<
feas
[
i
][
j
*
17
+
c
]
<<
", "
;
}
cout
<<
endl
;
cout
<<
endl
;
}
else
{
cout
<<
", "
;
}
}
for
(
int
j
=
0
;
j
<
pis
[
i
].
size
();
++
j
){
if
(
pis
[
i
][
j
]
!=
0
){
cout
<<
"("
<<
j
<<
"-"
<<
pis
[
i
][
j
]
<<
"), "
;
}
}
cout
<<
endl
<<
vs
[
i
]
<<
endl
;
}
}
return
0
;
return
0
;
}
}
c++/c++file/binary_read_write/main1.cpp
0 → 100644
View file @
fa95df52
// g++ main.cpp -std=c++11
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using
namespace
std
;
void
read
(
const
string
file
,
vector
<
vector
<
float
>
>&
feas
,
vector
<
vector
<
float
>
>&
pis
,
vector
<
float
>&
vs
){
int
steps_in
=
0
;
int
winner_in
=
0
;
char
feas_in
[
1000
*
17
*
19
*
19
];
float
pis_in
[
1000
*
362
];
std
::
ifstream
input
(
file
,
ios
::
in
|
ios
::
binary
);
input
.
read
((
char
*
)
&
steps_in
,
sizeof
(
int
));
// CHECK_LE(steps_in, 1000);
input
.
read
((
char
*
)
&
winner_in
,
sizeof
(
int
));
input
.
read
((
char
*
)
feas_in
,
sizeof
(
char
)
*
19
*
19
*
17
*
steps_in
);
input
.
read
((
char
*
)
pis_in
,
sizeof
(
float
)
*
(
19
*
19
+
1
)
*
steps_in
);
input
.
close
();
feas
.
resize
(
steps_in
);
pis
.
resize
(
steps_in
);
vs
.
resize
(
steps_in
);
for
(
int
step
=
0
;
step
<
steps_in
;
++
step
){
feas
[
step
].
resize
(
17
*
19
*
19
);
pis
[
step
].
resize
(
1
+
19
*
19
);
for
(
int
j
=
0
;
j
<
17
*
19
*
19
;
++
j
){
feas
[
step
][
j
]
=
feas_in
[
step
*
17
*
19
*
19
+
j
]
?
1.0
:
0.0
;
}
for
(
int
j
=
0
;
j
<
1
+
19
*
19
;
++
j
){
pis
[
step
][
j
]
=
pis_in
[
step
*
(
1
+
19
*
19
)
+
j
];
}
if
((
step
%
2
==
0
)
^
(
winner_in
==
1
)){
// self lose
vs
[
step
]
=
-
1.0
;
}
else
{
vs
[
step
]
=
1.0
;
}
}
}
void
write
(
const
string
file
){
int
steps_out
=
400
;
int
winner_out
=
1
;
// 1: black win, 0: white win
unsigned
char
*
feas_out
=
new
unsigned
char
[
steps_out
*
17
*
19
*
19
];
float
*
pis_out
=
new
float
[
steps_out
*
362
];
srand
((
int
)
time
(
0
));
for
(
int
i
=
0
;
i
<
steps_out
;
++
i
){
int
position_0
=
int
(
361.0
*
rand
()
/
(
RAND_MAX
+
1.0
));
int
position_1
=
int
(
361.0
*
rand
()
/
(
RAND_MAX
+
1.0
));
while
(
position_1
==
position_0
){
position_1
=
int
(
361.0
*
rand
()
/
(
RAND_MAX
+
1.0
));
}
for
(
int
c
=
0
;
c
<
16
;
++
c
){
feas_out
[
i
*
17
*
19
*
19
+
position_0
*
17
+
c
]
=
(
unsigned
char
)
1
;
}
for
(
int
c
=
0
;
c
<
2
;
++
c
){
feas_out
[
i
*
17
*
19
*
19
+
position_1
*
17
+
c
]
=
(
unsigned
char
)
1
;
}
pis_out
[
i
*
362
+
position_0
]
=
0.9
;
pis_out
[
i
*
362
+
position_1
]
=
0.1
;
for
(
int
j
=
0
;
j
<
19
*
19
;
++
j
){
feas_out
[
i
*
17
*
19
*
19
+
j
*
17
+
16
]
=
(
i
%
2
==
0
)
?
(
unsigned
char
)
1
:
(
unsigned
char
)
0
;
}
}
std
::
ofstream
output
(
file
,
ios
::
out
|
ios
::
binary
);
output
.
write
((
char
*
)
&
steps_out
,
sizeof
(
int
));
output
.
write
((
char
*
)
&
winner_out
,
sizeof
(
int
));
output
.
write
((
char
*
)
feas_out
,
sizeof
(
char
)
*
19
*
19
*
17
*
steps_out
);
output
.
write
((
char
*
)
pis_out
,
sizeof
(
float
)
*
(
19
*
19
+
1
)
*
steps_out
);
output
.
close
();
std
::
ofstream
finish_out
(
file
+
".finish"
,
ios
::
out
|
ios
::
binary
);
finish_out
.
close
();
}
int
main
(){
stringstream
ss
;
// for (int i = 0; i < 1000; ++i){
// ss.str("");
// ss << "go/" << i << ".bin";
// write(ss.str());
// }
vector
<
vector
<
float
>
>
feas
;
vector
<
vector
<
float
>
>
pis
;
vector
<
float
>
vs
;
read
(
"0.bin"
,
feas
,
pis
,
vs
);
cout
<<
feas
.
size
()
<<
", "
<<
pis
.
size
()
<<
", "
<<
vs
.
size
()
<<
", "
<<
endl
;
for
(
int
i
=
0
;
i
<
feas
.
size
();
++
i
){
cout
<<
"========"
<<
i
<<
"======"
<<
endl
;
for
(
int
j
=
0
;
j
<
19
*
19
;
++
j
){
cout
<<
j
<<
": "
;
for
(
int
c
=
0
;
c
<
17
;
++
c
){
cout
<<
feas
[
i
][
j
*
17
+
c
]
<<
", "
;
}
cout
<<
endl
;
}
for
(
int
j
=
0
;
j
<
pis
[
i
].
size
();
++
j
){
if
(
pis
[
i
][
j
]
!=
0
){
cout
<<
"("
<<
j
<<
"-"
<<
pis
[
i
][
j
]
<<
"), "
;
}
}
cout
<<
endl
<<
vs
[
i
]
<<
endl
;
}
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment