Skip to content

Instantly share code, notes, and snippets.

@DSCF-1224
Created July 18, 2021 04:32
Show Gist options
  • Save DSCF-1224/b95274dc10bd00db4ed7d34ee9ad2a63 to your computer and use it in GitHub Desktop.
Save DSCF-1224/b95274dc10bd00db4ed7d34ee9ad2a63 to your computer and use it in GitHub Desktop.
64bit版 Mersenne Twister(C言語での実装)の unsigned long 出力を signed long 出力に変換する。
g++ -std=c++17 -Wall --pedantic-errors -o ./main.exe ./main.cpp
gfortran -fbacktrace -fbounds-check -ffree-line-length-none -g -O0 -pedantic -std=f2008 -Wall -Werror -Wextra -o ./main.exe ./main.f90
#include <array>
// std::array
#include <cstdlib>
// EXIT_SUCCESS
#include <fstream>
// std::fstream
#include <iostream>
// std::cerr
#include <iomanip>
// std::setw
#include <string>
// std::string
const size_t length_sample_data = 1000;
const size_t num_digit = 20;
void check_length_sample_data (std::ifstream &ifstream_sample_data, const std::size_t &length_sample_data_true)
{
char buffer[21];
std::size_t length_sample_data_raw = 0;
while( ifstream_sample_data.getline( buffer, sizeof(buffer) ) )
{
++length_sample_data_raw;
}
if ( length_sample_data_raw < length_sample_data_true )
{
std::cerr << "The detected number of sample data is " << length_sample_data_raw << std::endl;
std::cerr << "The number of sample data must be equal to " << length_sample_data_true << std::endl;
}
ifstream_sample_data.clear();
ifstream_sample_data.seekg(0, std::ios::beg);
return;
}
int main (void)
{
const std::string filename_sample_data_txt = "genrand64_int64_unsigned.txt";
const std::string filename_sample_data_bin = "genrand64_int64_unsigned.bin";
// STEP.01
// Open the file which constains the sample data.
std::ifstream ifstream_sample_data(filename_sample_data_txt);
if( !ifstream_sample_data )
{
std::cerr << "Failed to open a file: " << filename_sample_data_txt << std::endl;
}
// STEP.02
// Check the number of recorded sample data.
check_length_sample_data( ifstream_sample_data, length_sample_data );
// STEP.03
// Read the sample data.
std::array<uint64_t, length_sample_data> value_sample_data;
for (std::size_t itr = 0; itr < length_sample_data; ++itr)
{
char buffer[num_digit + 1];
char *buffer_endptr;
ifstream_sample_data.getline( buffer, sizeof(buffer) );
value_sample_data.at(itr) = strtoul( buffer, &buffer_endptr, 10 );
/* print debug */
// std::cout << std::setw(num_digit) << value_sample_data.at(itr) << std::endl;
}
// STEP.04
// Close the file which contains the sample data.
ifstream_sample_data.close();
// STEP.05
// Open a file to write read sample data as binary.
std::ofstream ofstream_sample_data_bin(filename_sample_data_bin, std::ios::binary);
for (std::size_t itr = 0; itr < length_sample_data; ++itr)
{
ofstream_sample_data_bin.write( reinterpret_cast<char *>(&value_sample_data.at(itr)), sizeof(uint64_t) );
/* print debug */
// std::cout << std::setw(num_digit) << value_sample_data.at(itr) << std::endl;
}
// STEP.06
// Close the file.
ofstream_sample_data_bin.close();
// STEP.END
return EXIT_SUCCESS;
}
program main
! <module>s to import
use, intrinsic :: iso_fortran_env
! require all variables to be explicitly declared
implicit none
! constant(s) for this <program>
integer(INT32) , parameter :: len_sample_data = 1000_INT32
character(len=*) , parameter :: filename_read = 'genrand64_int64_unsigned.bin'
character(len=*) , parameter :: filename_save_bin = 'genrand64_int64_signed.bin'
character(len=*) , parameter :: filename_save_txt = 'genrand64_int64_signed.txt'
! variable(s) for this <program>
integer :: unit_read
integer :: unit_save_bin
integer :: unit_save_txt
integer(INT64) :: val_sample_data (len_sample_data)
! support variable(s) for this <program>
integer(INT32) :: itr_data
! STEP.01
! Open files to read/write the given sample data.
open(&!
newunit = unit_read , &!
access = 'stream' , &!
action = 'read' , &!
file = filename_read , &!
form = 'unformatted' , &!
status = 'old' &!
)
open(&!
newunit = unit_save_bin , &!
access = 'stream' , &!
action = 'write' , &!
file = filename_save_bin , &!
form = 'unformatted' , &!
status = 'replace' &!
)
open(&!
newunit = unit_save_txt , &!
action = 'write' , &!
file = filename_save_txt , &!
form = 'formatted' , &!
status = 'replace' &!
)
! STEP.02
! Read/Write the given sample data.
do itr_data = 1_INT32 , len_sample_data , 1_INT32
read ( unit= unit_read ) val_sample_data(itr_data)
write( unit= unit_save_bin ) val_sample_data(itr_data)
write( unit= unit_save_txt, fmt='(I20)', advance='yes' ) val_sample_data(itr_data)
end do
! STEP.03
! Close the opened files.
close( unit= unit_read , status= 'keep' )
close( unit= unit_save_bin , status= 'keep' )
close( unit= unit_save_txt , status= 'keep' )
end program main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment