F
freesoft12
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.
Here is the C++ program:
-----------------------------
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
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");
if (!fp)
{
printf("Error: Unable to open ./rw_binary.dat\n");
exit(1);
}
for (vector<string>::const_iterator
it(v.begin()),itEnd(v.end());it!=itEnd;++it)
{
unsigned size = it->size()+1;
fwrite(&size,sizeof(unsigned),1,fp);
fwrite(it->c_str(),sizeof(char),size,fp);
}
fclose(fp);
}
void
read_binary()
{
FILE *fp = fopen("rw_binary.dat","r");
while (!feof(fp))
{
unsigned size(0);
fread(&size,sizeof(unsigned),1,fp);
char *buf = (char*)malloc(size*sizeof(char));
fread(buf,sizeof(char),size,fp);
printf("%s\n",buf);
free(buf);
}
fclose(fp);
}
int main()
{
write_binary();
read_binary();
return 0;
}
----------------------
I wanted to create a Perl program that mimics the 'read_binary()'. But
I am getting a perl warning ( I have the 'use strict & diagnostics').
Here it is:
-----------------------
eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec
perl -w -S $0 $argv:q' if 0;
#
use strict;
use diagnostics;
open(IN,"/home/rradhakr/progs/rw_binary.dat") or die "Cannot open
rw_binary.dat\n";
binmode(IN);
while (<IN>) {
my $buf = 0;
read(IN,$buf,6);
my $string = undef;
read(IN,$string,$size);
print "$size\n";
}
close(IN);
-------------------------
Here is the error I get Perl:
Argument "" isn't numeric in read at read_binary_file.pl line 13, <IN>
line 1 (#1)
(W numeric) The indicated string was fed as an argument to an
operator that expected a numeric value instead. If you're fortunate
the message will identify which operator was so unfortunate.
Any suggestions?
Regards
John
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.
Here is the C++ program:
-----------------------------
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
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");
if (!fp)
{
printf("Error: Unable to open ./rw_binary.dat\n");
exit(1);
}
for (vector<string>::const_iterator
it(v.begin()),itEnd(v.end());it!=itEnd;++it)
{
unsigned size = it->size()+1;
fwrite(&size,sizeof(unsigned),1,fp);
fwrite(it->c_str(),sizeof(char),size,fp);
}
fclose(fp);
}
void
read_binary()
{
FILE *fp = fopen("rw_binary.dat","r");
while (!feof(fp))
{
unsigned size(0);
fread(&size,sizeof(unsigned),1,fp);
char *buf = (char*)malloc(size*sizeof(char));
fread(buf,sizeof(char),size,fp);
printf("%s\n",buf);
free(buf);
}
fclose(fp);
}
int main()
{
write_binary();
read_binary();
return 0;
}
----------------------
I wanted to create a Perl program that mimics the 'read_binary()'. But
I am getting a perl warning ( I have the 'use strict & diagnostics').
Here it is:
-----------------------
eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec
perl -w -S $0 $argv:q' if 0;
#
use strict;
use diagnostics;
open(IN,"/home/rradhakr/progs/rw_binary.dat") or die "Cannot open
rw_binary.dat\n";
binmode(IN);
while (<IN>) {
my $buf = 0;
read(IN,$buf,6);
my $string = undef;
read(IN,$string,$size);
print "$size\n";
}
close(IN);
-------------------------
Here is the error I get Perl:
Argument "" isn't numeric in read at read_binary_file.pl line 13, <IN>
line 1 (#1)
(W numeric) The indicated string was fed as an argument to an
operator that expected a numeric value instead. If you're fortunate
the message will identify which operator was so unfortunate.
Any suggestions?
Regards
John