variable-length strings

U

Uno

Hello from the land of enchantment,

I begin a crosspost herewith that I hope to make relevant to the
newsgroups listed. It will take some time to get to there from here, but
the ambition is to make it relatively lean with the topicality of each
ng addressed and respected. I am OP; if that's a problem for you, I'd
like you to move on graciously.

It begins with perl, which I am currently reading. So that's gonna be
q1). There will be more questions in the same format, and I would like
to make you aware of that as you consider an editorial choice. OP
suggests editorial choices that derive from the questions, that is, are
trimmed with respect to q.

$ pwd
/media/KINGSTON
$ perl marni3.pl
death
$ cat marni3.pl
#!/usr/bin/perl -w
use Net::FTP;
my $domain = 'www.merrillpjensen.com';
my $username = 'u61210220';
my $password = '';
my $file = 'jac1.avi';


$ftp = Net::FTP->new($domain) or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";

$ftp->cwd('/wsb6326330301/resources/') or die "death $@\n";
$ftp->put($file) or die "put failed ", $ftp->message;

$

q1) Why did I die?

I had a very successful day. I could show it, except that I'm unable to
communicate well with the internet. This is usually the case when I try
to extend my ubuntu capability. I have something that hangs that
wouldn't under a more firmly-established identity.

q2) [I imagine that I have a distinct question for each listed ng] In
testing, I've noticed a difference in file sizes between what I upload
with ftp and what I download. The files are 60 megs, conceived by
apple, and I can't show what I'm saying until I get ftp working.

There's no way I can look at these files with a hex editor and my eyes.
Doesn't unix have a built-in way to compare them?

q3)

a) Is there some newsgroup out there that considers web design, and not
the kind where you have to give them money for their opinion. If I paid
$40 for usenet advice, it might be worse than what a hooker on central
might do; it's given by people who want to make $40 on usenet.

alt.web.css.embedded.object.rus ?

q4) and then the good people of c.l.c. It looks like your tone has
come a long way. Down the road I'd like to consider how to pass
information which is perl input to fortran output using the iso c binding.

Cheers,
 
G

George Mpouras

try to change the connection line to

$ftp = Net::FTP->new($domain, Debug=>1, Passive=>1)
 
C

ccc31807

q1)  Why did I die?

The simplest place to start is with your command line ftp program. If
you can connect and transfer files with your command line ftp, then IN
THEORY you should be able to put the same series of commands in a
batch file (i.e., a Perl script) and run them. If you can do that,
then you have all the functionality you neet./

q2) [I imagine that I have a distinct question for each listed ng]  In
There's no way I can look at these files with a hex editor and my eyes.
  Doesn't unix have a built-in way to compare them?

On Unix/Linux, use diff. If you are comfortable with vi/vim, it also
has a diff capability.
q3)

a)  Is there some newsgroup out there that considers web design, and not
the kind where you have to give them money for their opinion.  If I paid
$40 for usenet advice, it might be worse than what a hooker on central
might do; it's given by people who want to make $40 on usenet.

Sorry, but you need to rephrase your question, as this does not make
sense to me.
q4)  and then the good people of c.l.c.  It looks like your tone has
come a long way.  Down the road I'd like to consider how to pass
information which is perl input to fortran output using the iso c binding..

Data is data. You can use Perl to manipulate and format data. In fact,
PERL (in the opinion of some, anyway) stands for Practical Extraction
and Reporting Language.

CC.
 
P

Peter J. Holzer

["Followup-To:" header set to comp.lang.perl.misc.]
$ perl marni3.pl
death
$ cat marni3.pl
#!/usr/bin/perl -w
use Net::FTP;
my $domain = 'www.merrillpjensen.com';
my $username = 'u61210220';
my $password = '';
my $file = 'jac1.avi';


$ftp = Net::FTP->new($domain) or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";

$ftp->cwd('/wsb6326330301/resources/') or die "death $@\n";
$ftp->put($file) or die "put failed ", $ftp->message;

$

q1) Why did I die?

Simple answer: Because you couldn't change the current working directory
to '/wsb6326330301/resources/'.

As to why that didn't work, the Net::FTP module would tell you if you
asked it. Read the synopsis of perldoc Net::FTP for a few examples on
printing proper error messages.

hp
 
P

Peter J. Holzer

["Followup-To:" header set to comp.unix.shell.]
q2) [I imagine that I have a distinct question for each listed ng] In
testing, I've noticed a difference in file sizes between what I upload
with ftp and what I download. The files are 60 megs, conceived by
apple, and I can't show what I'm saying until I get ftp working.

There's no way I can look at these files with a hex editor and my eyes.
Doesn't unix have a built-in way to compare them?

There's cmp, but it only shows you where the first difference is.

There's diff, but it is intended for text files.

You could convert both files with od or hd and diff those. But that
doesn't work well.

I think Emacs has a binary-diff mode, but I'm from the vi fraction.

So, back in the mid 1990's I wrote bdiff (binary diff):
http://www.hjp.at/programs/bdiff/

I guess I should dust it off, write a man-page and try to get it added
to some linux distribution. But I very rarely need it myself.

hp
 
U

Uno

The simplest place to start is with your command line ftp program. If
you can connect and transfer files with your command line ftp, then IN
THEORY you should be able to put the same series of commands in a
batch file (i.e., a Perl script) and run them. If you can do that,
then you have all the functionality you neet./

I needed a fresh day and fresh eyes, and t set Debug=> 2 to see that the
wsb "web site builder" number was wrong. That's a separate directory
that my host uses for when they build the website. I want to adios this
thing, so I called them, and they have you change an administrative
setting so that a browser will be directed to the index.html in the root
menu as opposed to in the wsb folder.

So now this /wsb.../ thing is a relic for me to play with and destroy.
Why didn't this work:

[sorry can only paste as quotation:]
$ perl marni5.pl
Net::FTP>>> Net::FTP(2.77)
Net::FTP>>> Exporter(5.63)
Net::FTP>>> Net::Cmd(2.29)
Net::FTP>>> IO::Socket::INET(1.31)
Net::FTP>>> IO::Socket(1.31)
Net::FTP>>> IO::Handle(1.28)
Net::FTP=GLOB(0xa07a7d8)<<< 220 FTP Server ready.
Net::FTP=GLOB(0xa07a7d8)>>> USER u61210220
Net::FTP=GLOB(0xa07a7d8)<<< 331 Password required for u61210220
Net::FTP=GLOB(0xa07a7d8)>>> PASS ....
Net::FTP=GLOB(0xa07a7d8)<<< 230 User u61210220 logged in
Net::FTP=GLOB(0xa07a7d8)>>> RMD /wsb6121022001/
Net::FTP=GLOB(0xa07a7d8)<<< 550 /wsb6121022001/: Directory not empty
Net::FTP=GLOB(0xa07a7d8)>>> PORT 192,168,0,64,142,193
Net::FTP=GLOB(0xa07a7d8)<<< 200 PORT command successful
Net::FTP=GLOB(0xa07a7d8)>>> NLST /wsb6121022001/
Net::FTP=GLOB(0xa07a7d8)<<< 150 Opening ASCII mode data connection for file list
Net::FTP=GLOB(0xa07a7d8)<<< 226 Transfer complete
Net::FTP=GLOB(0xa07a7d8)>>> DELE /wsb6121022001/.
Net::FTP=GLOB(0xa07a7d8)<<< 550 /wsb6121022001/.: Is a directory
Net::FTP=GLOB(0xa07a7d8)>>> RMD /wsb6121022001/.
Net::FTP=GLOB(0xa07a7d8)<<< 550 /wsb6121022001/.: Directory not empty
Net::FTP=GLOB(0xa07a7d8)>>> PORT 192,168,0,64,186,213
Net::FTP=GLOB(0xa07a7d8)<<< 200 PORT command successful
Net::FTP=GLOB(0xa07a7d8)>>> NLST /wsb6121022001/.
Net::FTP=GLOB(0xa07a7d8)<<< 150 Opening ASCII mode data connection for file list
Net::FTP=GLOB(0xa07a7d8)<<< 226 Transfer complete

....

it just keeps on telling me Directory not empty
^C
$ cat marni5.pl
#!/usr/bin/perl -w
use Net::FTP;
my $domain = 'www.merrillpjensen.com';
my $username = 'u61210220';
my $password = '';

$ftp = Net::FTP->new($domain, Debug => 2) or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";

my $recurse = 1;
$ftp->rmdir('/wsb6121022001/', $recurse);
#$ftp->cwd('/wsb6121022001/') or die "death $@\n";

my @file = $ftp->ls();

foreach $file (@file){
print "$file\n";
}
$

Cpan says that RECURSE has to be true, and I thought a good guess for
"true" in a syntax that is a child of C would be one.
q2) [I imagine that I have a distinct question for each listed ng] In
There's no way I can look at these files with a hex editor and my eyes.
Doesn't unix have a built-in way to compare them?

On Unix/Linux, use diff. If you are comfortable with vi/vim, it also
has a diff capability.

Thx, ccc, I'll do that. Here's what's vexing me right now.

http://www.merrillpjensen.com/images/jac1.bmp

The only difference between these files, is that the smaller one went on
a round on the internet. It was sent up like this:
#!/usr/bin/perl -w
use strict;
use Net::FTP;
my $domain = 'www.merrillpjensen.com';
my $username = 'u61210220';
my $password = '';
my $file = 'jac1.bmp';
my $dir = '/images/';

my $ftp = Net::FTP->new($domain, Debug =>2) or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";
$ftp->mkdir($dir) or die "death: $@\n";
$ftp->cwd($dir) or die "death: $@\n";
$ftp->put($file) or die "put failed ", $ftp->message;

And downloaded using google chrome. Is this typical, because if my
experience is, then I don't really see how any large file gets
transferred faithfully.

Gotta go pull down some legal tender. Adios.
 
U

Uno

So, back in the mid 1990's I wrote bdiff (binary diff):
http://www.hjp.at/programs/bdiff/

I guess I should dust it off, write a man-page and try to get it added
to some linux distribution. But I very rarely need it myself.
[...]
So I downloaded your software, extracted it on a thumbdrive, compiled
it, told ubuntu to run it, and it told me I don't have permission.

$ sudo chmod 777 bdiff
$ ls -l

-rw-r--r-- 1 dan dan 12414 2011-06-02 16:29 bdiff

That was my last try with that executable. So I tried it again.

$ pwd
/media/KINGSTON/bdiff-1.0
^^^^^^^^^^^^^^^^

What kind of file system is this and how is it mounted?
$ cc -Wall -Wextra bdiff.c -o bdiff2
bdiff.c: In function ‘readfile’:
bdiff.c:98: warning: comparison between signed and unsigned integer
expressions

The warnings are harmless, AFAICS, and partially misleading (comparison
between size_t and -1 is well-defined as long as sizeof(size_t)>=
sizeof(int), which is pretty much guaranteed).

hp

I want to pursue this on the C side for a bit. [x-posted to clc]

$ pwd
/home/dan/source/bdiff-1.0
$ cc -Wall -Wextra cmp1.c -o cmp
cmp1.c: In function ‘main’:
cmp1.c:12: warning: comparison is always true due to limited range of
data type
cmp1.c:17: warning: implicit declaration of function ‘close’
cmp1.c:18: error: expected declaration or statement at end of input
$ cat cmp1.c

#include <stdio.h>

int main(void)
{
unsigned char c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)
{
counter++;
}
printf("Counter reached %ld\n", counter);
close (fp);
return 0;
// cc -Wall -Wextra cmp1.c -o cmp
$

What obvious thing am I missing here?
 
I

Ian Collins

I want to pursue this on the C side for a bit. [x-posted to clc]

$ pwd
/home/dan/source/bdiff-1.0
$ cc -Wall -Wextra cmp1.c -o cmp
cmp1.c: In function ‘main’:
cmp1.c:12: warning: comparison is always true due to limited range of
data type
cmp1.c:17: warning: implicit declaration of function ‘close’
cmp1.c:18: error: expected declaration or statement at end of input
$ cat cmp1.c

#include<stdio.h>

int main(void)
{
unsigned char c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)

I'm guessing this is line 12. fgetc returns an int. EOF is an integer
constant.
{
counter++;
}
printf("Counter reached %ld\n", counter);
close (fp);

return 0;

Missing } ?
 
U

Uno

I want to pursue this on the C side for a bit. [x-posted to clc]

$ pwd
/home/dan/source/bdiff-1.0
$ cc -Wall -Wextra cmp1.c -o cmp
cmp1.c: In function ‘main’:
cmp1.c:12: warning: comparison is always true due to limited range of
data type
cmp1.c:17: warning: implicit declaration of function ‘close’
cmp1.c:18: error: expected declaration or statement at end of input
$ cat cmp1.c

#include<stdio.h>

int main(void)
{
unsigned char c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)

I'm guessing this is line 12. fgetc returns an int. EOF is an integer
constant.
{
counter++;
}
printf("Counter reached %ld\n", counter);
close (fp);

return 0;

Missing } ?

Thx, Ian.

$ cc -Wall -Wextra cmp2.c -o cmp
$ ./cmp
Counter reached 19573712
$ ls -l
....
-rw-r--r-- 1 dan dan 19573712 2011-06-05 17:32 shoulder.wmv
$ cat cmp2.c

#include <stdio.h>

int main(void)
{
int c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)
{
counter++;
}
printf("Counter reached %ld\n", counter);
fclose (fp);
return 0;
}
// cc -Wall -Wextra cmp2.c -o cmp
$

I guess I thought with counter starting at zero that the numbers
wouldn't agree exactly, but they do.

It's a weird night here in abq; I'm gonna go out and howl at the orange
moon.
 
U

Uno

I want to pursue this on the C side for a bit. [x-posted to clc]

$ pwd
/home/dan/source/bdiff-1.0
$ cc -Wall -Wextra cmp1.c -o cmp
cmp1.c: In function ‘main’:
cmp1.c:12: warning: comparison is always true due to limited range of
data type
cmp1.c:17: warning: implicit declaration of function ‘close’
cmp1.c:18: error: expected declaration or statement at end of input
$ cat cmp1.c

#include<stdio.h>

int main(void)
{
unsigned char c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)

I'm guessing this is line 12. fgetc returns an int. EOF is an integer
constant.
{
counter++;
}
printf("Counter reached %ld\n", counter);
close (fp);

return 0;

Missing } ?

Thx, Ian.

$ cc -Wall -Wextra cmp2.c -o cmp
$ ./cmp
Counter reached 19573712
$ ls -l
...
-rw-r--r-- 1 dan dan 19573712 2011-06-05 17:32 shoulder.wmv
$ cat cmp2.c

#include <stdio.h>

int main(void)
{
int c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)
{
counter++;
}
printf("Counter reached %ld\n", counter);
fclose (fp);
return 0;
}
// cc -Wall -Wextra cmp2.c -o cmp
$

I guess I thought with counter starting at zero that the numbers
wouldn't agree exactly, but they do.

It's a weird night here in abq; I'm gonna go out and howl at the orange
moon.

I'm stuck again, so that's the night. I don't see why this wouldn't
traverse both files for the length of the shorter one:

$ cc -Wall -Wextra cmp4.c -o cmp
cmp4.c: In function ‘main’:
cmp4.c:12: error: expected statement before ‘)’ token
$ cat cmp4.c
#include <stdio.h>

int main(void)
{
int c, d;
long counter = 0;
FILE *fp, *fq;

fp=fopen("shoulder.wmv", "rb+");
fq=fopen("downloaded1.wmv", "rb+");

while (((c = fgetc(fp)) != EOF) || (d = fgetc(fq)) != EOF))
{
counter++;
if (counter == 10000)
{
printf("int is %d\n", c);
}

if ( c != d)
{
printf("Ungleich: %ld\n", counter);
}
}
printf("Counter reached %ld\n", counter);
fclose (fp);
return 0;
}
// cc -Wall -Wextra cmp4.c -o cmp
$

My parens are probably mismatched by now, but I don't make one statement
on that line; I think I make two.
 
J

Janis Papanagnou

Am 09.06.2011 10:57, schrieb Uno:
On 06/ 9/11 03:59 PM, Uno wrote:

I want to pursue this on the C side for a bit. [x-posted to clc]

$ pwd
/home/dan/source/bdiff-1.0
$ cc -Wall -Wextra cmp1.c -o cmp
cmp1.c: In function ‘main’:
cmp1.c:12: warning: comparison is always true due to limited range of
data type
cmp1.c:17: warning: implicit declaration of function ‘close’
cmp1.c:18: error: expected declaration or statement at end of input
$ cat cmp1.c

#include<stdio.h>

int main(void)
{
unsigned char c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)

I'm guessing this is line 12. fgetc returns an int. EOF is an integer
constant.

{
counter++;
}
printf("Counter reached %ld\n", counter);
close (fp);

You are missing <unistd.h>. If you had included it, your compile would
have failed. close works with a file descriptor, not a FILE*.

return 0;

Missing } ?

Thx, Ian.

$ cc -Wall -Wextra cmp2.c -o cmp
$ ./cmp
Counter reached 19573712
$ ls -l
...
-rw-r--r-- 1 dan dan 19573712 2011-06-05 17:32 shoulder.wmv
$ cat cmp2.c

#include <stdio.h>

int main(void)
{
int c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)
{
counter++;
}
printf("Counter reached %ld\n", counter);
fclose (fp);
return 0;
}
// cc -Wall -Wextra cmp2.c -o cmp
$

I guess I thought with counter starting at zero that the numbers
wouldn't agree exactly, but they do.

It's a weird night here in abq; I'm gonna go out and howl at the orange
moon.

I'm stuck again, so that's the night. I don't see why this wouldn't
traverse both files for the length of the shorter one:

Here you seem to be asking why your algorithm doesn't work,
but below you show us an apparent compiler syntax error;
your parenthesis are not balanced in line 12.
$ cc -Wall -Wextra cmp4.c -o cmp
cmp4.c: In function ‘main’:
cmp4.c:12: error: expected statement before ‘)’ token
$ cat cmp4.c
#include <stdio.h>

int main(void)
{
int c, d;
long counter = 0;
FILE *fp, *fq;

fp=fopen("shoulder.wmv", "rb+");
fq=fopen("downloaded1.wmv", "rb+");

while (((c = fgetc(fp)) != EOF) || (d = fgetc(fq)) != EOF))

while ( (c = fgetc(fp)) != EOF
|| (d = fgetc(fq)) != EOF )
{
counter++;
if (counter == 10000)
{
printf("int is %d\n", c);
}

if ( c != d)
{
printf("Ungleich: %ld\n", counter);
}
}
printf("Counter reached %ld\n", counter);
fclose (fp);
return 0;
}
// cc -Wall -Wextra cmp4.c -o cmp
$

My parens are probably mismatched by now, but I don't make one statement
on that line; I think I make two.

The compiler doesn't know what your intention was; it's irrelevant
how many statements you have. Fix your syntax.

BTW, why are you posting this to comp.unix.shell?

Janis
 
E

Eric Sosman

$ pwd
/home/dan/source/bdiff-1.0
$ cc -Wall -Wextra cmp1.c -o cmp
cmp1.c: In function ‘main’:
cmp1.c:12: warning: comparison is always true due to limited range of
data type
cmp1.c:17: warning: implicit declaration of function ‘close’
cmp1.c:18: error: expected declaration or statement at end of input
$ cat cmp1.c

#include <stdio.h>

int main(void)
{
unsigned char c;
long counter = 0;
FILE *fp;

fp=fopen("shoulder.wmv", "rb+");

while ((c = fgetc(fp)) != EOF)
{
counter++;
}
printf("Counter reached %ld\n", counter);
close (fp);
return 0;
// cc -Wall -Wextra cmp1.c -o cmp
$

What obvious thing am I missing here?

First, that an `unsigned char', necessarily non-negative,
cannot be equal to the negative value EOF. (Let's ignore
exotic systems where sizeof(int)==1, okay?)

Second, that fclose() is spelled with an initial "f". By
writing it as close(), you've f-ed up.

Third, that some C delimiters are used in matching pairs.
Just as each ( needs a matching ), so each { needs a matching }.
 
U

Uno

The compiler doesn't know what your intention was; it's irrelevant
how many statements you have. Fix your syntax.

Thx, janis. I have it syntactical now, but the behavior has a ways to
come. Why does my program think that the first byte's are differing,
while the unix command thinks that only occurs much later:

$ cc -Wall -Wextra cmp4.c -o cmp
$ ./cmp
Ungleich: 1
c and d are 48 and 134514059
Counter reached 1
$ cmp shoulder.wmv downloaded1.wmv
shoulder.wmv downloaded1.wmv differ: byte 196502, line 1409
$ cat cmp4.c
#include <stdio.h>

int main(void)
{
int c, d;
long counter = 0;
FILE *fp, *fq;

fp = fopen("shoulder.wmv", "rb+");
fq = fopen("downloaded1.wmv", "rb+");

while (((c = fgetc(fp)) != EOF) || ((d = fgetc(fq)) != EOF)) {
counter++;
if (counter == 10000) {
printf("int is %d\n", c);
}

if (c != d) {
printf("Ungleich: %ld\n", counter);
printf("c and d are %d and %d\n", c, d);
break;
}
}
printf("Counter reached %ld\n", counter);
fclose(fp);
return 0;
}

// cc -Wall -Wextra cmp4.c -o cmp
$
BTW, why are you posting this to comp.unix.shell?

*nix is my platform, and whenever I'm cooking up a project with a
programming language, I hit at least one big gotcha with the OS. Also,
I'm trying to imitate the functionality of cmp somewhat.

Are you suggesting that another unix group might be more appropriate?
 
U

Uno

On 6/8/2011 11:59 PM, Uno wrote:

First, that an `unsigned char', necessarily non-negative,
cannot be equal to the negative value EOF. (Let's ignore
exotic systems where sizeof(int)==1, okay?)

Right. I wish I could say this was the only time I'd forgotten that. I
could blame Heathfield for making me think that unsigned chars were what
I wanted to deal with binary data, but at some point, one owns his own
misperceptions.
Second, that fclose() is spelled with an initial "f". By
writing it as close(), you've f-ed up.

Third, that some C delimiters are used in matching pairs.
Just as each ( needs a matching ), so each { needs a matching }.

[OT]

Is that forest fire gonna affect AZ population centers? It's turning
into a bummer for NM. How can it still be zero percent contained?
 
J

James Waldby

.
Why does my program think that the first byte's are differing,
while the unix command thinks that only occurs much later:

$ cc -Wall -Wextra cmp4.c -o cmp
$ ./cmp
Ungleich: 1
c and d are 48 and 134514059
....

By C evaluation rules, both || and && will "shortcut" when
not all terms need to be evaluated. For example, in
expression (1 || xyz), xyz will never be evaluated.
while (((c = fgetc(fp)) != EOF) || ((d = fgetc(fq)) != EOF)) {
....
 
A

Angel

Thx, janis. I have it syntactical now, but the behavior has a ways to
come. Why does my program think that the first byte's are differing,
while the unix command thinks that only occurs much later:

Problem is here:
while (((c = fgetc(fp)) != EOF) || ((d = fgetc(fq)) != EOF)) {

If the left operand of the || operator evaluates to true, the right
operand is not evaluated, because the expression is already known to be
true as a whole.

In other words, as long as the value assigned to c != 0, d is never read,
and thus d is used uninitialized in the rest of the code.
 
B

Ben Pfaff

Uno said:
while (((c = fgetc(fp)) != EOF) || ((d = fgetc(fq)) != EOF)) {

You want to read one byte from each file on every loop iteration,
but this will only read from fq if the read from fp fails.

Try something more like this:

for (;;) {
c = fgetc(fp);
d = fgetc(fq);
if (c == EOF || d == EOF)
break;
 
K

Keith Thompson

Uno said:
Right. I wish I could say this was the only time I'd forgotten that. I
could blame Heathfield for making me think that unsigned chars were what
I wanted to deal with binary data, but at some point, one owns his own
misperceptions.

unsigned chars *are* what you should use to deal with binary data.

Your problem was that you were assigning the result of fgetc()
to an unsigned char object. Using a signed char or a plain char
would also have been wrong. fgetc() returns an int. Read your
documentation to see how that int value is generated.

I'm dropping comp.unix.shell from the cross-post.
 
U

Uno

You want to read one byte from each file on every loop iteration,
but this will only read from fq if the read from fp fails.

Try something more like this:

for (;;) {
c = fgetc(fp);
d = fgetc(fq);
if (c == EOF || d == EOF)
break;

Thx Ben. I knew that the usual idiom wouldn't work for files of
different sizes, but with the short-circuiting, it didn't work at all.

$ cc -Wall -Wextra cmp5.c -o cmp
$ ./cmp
counter, c, and d are: 196500 236 236
counter, c, and d are: 196501 108 108
counter, c, and d are: 196502 13 10
counter, c, and d are: 196503 10 122
counter, c, and d are: 196504 122 185
counter, c, and d are: 196505 185 113
counter, c, and d are: 196506 113 83
counter, c, and d are: 196507 83 106
counter, c, and d are: 196508 106 149
counter, c, and d are: 196509 149 174
counter, c, and d are: 196510 174 77
Counter reached 19573258
$ cat cmp5.c
#include <stdio.h>

int main(void)
{
int c, d;
long counter = 0;
FILE *fp, *fq;

fp = fopen("shoulder.wmv", "rb+");
fq = fopen("downloaded1.wmv", "rb+");

for (;;) {
c = fgetc(fp);
d = fgetc(fq);
if (c == EOF || d == EOF)
break;
counter++;
if ((counter >= 196500) && (counter <= 196510)) {
printf("counter, c, and d are: %ld %d %d\n", counter, c, d);
}
}
printf("Counter reached %ld\n", counter);
fclose(fp);
return 0;
}

// cc -Wall -Wextra cmp5.c -o cmp
$

I'm trying to get my head around what Alan Curry wrote about this:

"Octal 15 (ASCII CR) inserted before octal 12 (ASCII LF). Classical
text-mode FTP corruption."

Can someone say a few more words about what ftp may have done here?
 
U

Uno

Problem is here:


If the left operand of the || operator evaluates to true, the right
operand is not evaluated, because the expression is already known to be
true as a whole.

In other words, as long as the value assigned to c != 0, d is never read,
and thus d is used uninitialized in the rest of the code.

Thx, james and angel. Somehow, my only reference handy is a german
language K&R2, and I don't see a mention of the short-circuiting of
ODER, although, of course, it is well-known (by you maybe more than me).

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top