Prev: help with big numbers and DBI
Next: FAQ 4.8 How do I perform an operation on a series of integers?
From: sln on 9 Feb 2010 20:29 On Tue, 9 Feb 2010 11:16:30 -0800 (PST), "freesoft12(a)gmail.com" <freesoft12(a)gmail.com> wrote: >Hi, > >I have a written a C++ program that writes a set of paths into a >binary file. In the program, the 'write_binary()', writes to the >binary file and the 'read_binary()' opens the binary file, reads the >data and prints it out. > [your code snipped] It looks like you are dealing with strings and is a simple case of writing the length and string combo's. If thats all your doing, the solution is to mitigate machine dependencies. There's really no need to travel down the *binary* highway unless you have to. The Perl itself couldn't be simpler, its almost a one liner. -sln ----------------------------- // JBin.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; void write_binary(); void read_binary(); int _tmain(int argc, _TCHAR* argv[]) { write_binary(); read_binary(); system("perl ../jbin.pl"); return 0; } // void write_binary() { vector<string> v; v.push_back( "/a/b/c/d"); v.push_back( "/e/f"); v.push_back( "../h"); FILE *fp = fopen( "rw_binary.dat", "w"); // wb ?? if (!fp) { printf( "Error: Unable to open ./rw_binary.dat for write\n"); exit(1); } char buf[100]; for( vector<string>::const_iterator it(v.begin()),itEnd(v.end()); it!=itEnd; it++) { strcat( itoa( it->size(), buf, 10), "\n"); fputs( buf, fp); fputs( it->c_str(), fp); } fclose(fp); } // void read_binary() { FILE *fp = fopen( "rw_binary.dat", "r"); // rb ?? if (!fp) { printf( "Error: Unable to open ./rw_binary.dat for read\n"); exit(1); } printf( "\nFrom C++ ...\n"); char buf[100]; while (!feof(fp) && fgets( buf, 100, fp)) { size_t count = atoi( buf); char *sdata = (char *)malloc( count+1); memset( sdata, 0, count+1); fread( sdata, count, 1, fp); printf( "'%s'\n", sdata); free( sdata); } fclose(fp); } /* # Jbin.pl use strict; use warnings; open (my $fh, '<', 'rw_binary.dat') or die "Cannot open 'rw_binary.dat' for read: $!"; print "\nFrom perl ...\n"; my $buf; while (<$fh>) { read ($fh, $buf, $_); print "'$buf'\n"; } close($fh); __END__ */ /* Console output: From C++ ... '/a/b/c/d' '/e/f' '../h' From perl ... '/a/b/c/d' '/e/f' '../h' Press any key to continue . . . */
From: Peter Makholm on 10 Feb 2010 02:03 "freesoft12(a)gmail.com" <freesoft12(a)gmail.com> writes: > Sorry about that, I posted the Perl program that I was playing with. > Here is the orig Perl program: Many of the comments made by Peter J. Holzer and me still apply. //Makholm
First
|
Prev
|
Pages: 1 2 Prev: help with big numbers and DBI Next: FAQ 4.8 How do I perform an operation on a series of integers? |