// created by Justin C. Miller
// created on 10-1-2001
// created for http://www.geocities.com/neonprimetime.geo/index.html
// CONVERT A TEXT FILE TO A BINARY FILE
#include
#include // necessary for file input and output (ifstream, ofstream, fstream)
#include
#include
using namespace std ;
ifstream text_in ;
ifstream binary_in ;
ofstream text_out ;
ofstream binary_out ;
void createTextFile(int) ;
void readTextFile(int *, int) ;
void writeToBinaryFile(int *, int) ;
void displayBinaryFile(int) ;
int main() {
int size = 100 ; // number of integers
int arrayOfData[size] ; // where you're storing your data
createTextFile(size) ; // create a text file with random integers in it
readTextFile(arrayOfData, size) ; // read those random integers into the array
writeToBinaryFile(arrayOfData, size) ; // write the numbers in the array to the binary file
displayBinaryFile(size) ; //read the numbers in the binary file and display them to the screen
return 0 ;
}
// send in a size (number of random numbers you want) and this will create
// a text file called myText.dat with that many random integers in the range 1 to 99
void createTextFile(int size) {
text_out.open("myText.dat", ios::out) ; // open text file myText.dat for output
srand(time(0)) ; //randomize
for(int i = 0 ; i < size ; i++) // store "size" random numbers into myText.dat
text_out << rand() % 99 + 1 << " ";
text_out.close() ; // close myText.dat
}
// send in an array and a size of the array
// assumes there are "size" numbers in the myText.dat
// reads them in and stores them in the array
void readTextFile(int * array, int size) {
text_in.open("myText.dat", ios::in) ; // open text file myText.dat for input
for(int i = 0 ; i < size ; i++) //assuming myText.dat has "size" numbers in it, read them all into the array
text_in >> array[i] ;
text_in.close() ; // close myText.dat
}
// sends in an array filled with "size" number of integers
// writes the array to the binary file myBin.dat
void writeToBinaryFile(int * array, int size) {
binary_out.open("myBin.dat", ios::out|ios::binary) ; // open binary file, myBin.dat for output
int current = 0 ; // the current position you start at is zero (eg. the beginning of the file)
binary_out.seekp(current) ; // seekp places the writing pointer to the current spot which is zero or the beginning of the file
for(int i = 0 ; i < size ; i++) { // assuming there are "size" number of integers to write out, write each out, incrementing current position each time and adjusting the write pointer
binary_out.write(reinterpret_cast(&array[i]), sizeof(array[i])) ;
current++ ;
binary_out.seekp(current * sizeof(array[i])) ;
}
binary_out.close() ; // close myBin.dat
}
// sends in a size (or number of integers in the binary file myBin.dat)
// reads in all the integers and prints them to the screen
void displayBinaryFile(int size) {
binary_in.open("myBin.dat", ios::in|ios::binary) ; // open binary file myBin.dat for input
int current = 0 ; // the current position you start at is zero (eg the beginning of the file)
binary_in.seekg(current) ; // the seekg sets the read pointer to the beginning of the file (eg current)
int number ; // number is the place we're going to store the number we read in and display on the screen
binary_in.read(reinterpret_cast(&number), sizeof(number)) ; // read the first number in from myBin.dat
while(current <= size){ // while current position has not reached the size (eg. the end of the file) keep reading in numbers and printing them to the screen
cout << number << " " ;
binary_in.read(reinterpret_cast(&number), sizeof(number)) ;
current++ ;
binary_in.seekg(current * sizeof(number)) ;
}
binary_in.close() ; // close myBin.dat
}
               (
geocities.com/neonprimetime.geo/cpp)                   (
geocities.com/neonprimetime.geo)