tried to compace same file

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hi,
   I tried to compare same file on linux. for this i did
  while(read(fd1, ptr1, 1)  || read(fd2, ptr2, 1))  {
....
....
....
}

but in this case only first read works. some one told me compiler is
doing optimization then  I compiled it with -O0 option but even then
its giving same result. Can any one tell me why so . and what option
should I give to compile so that I can read both the file in same
whilel

Regards
  Sumit Shrivastava


Re: tried to compace same file

Quoted text here. Click to load it

You are either joking, or in desperate need of "Intro to C
Programming".

Quoted text here. Click to load it

Someone either didn't see your code, or is just as clueless as
you are.

Quoted text here. Click to load it

As it should.

Quoted text here. Click to load it

Because the C language standard requires it be so.

Quoted text here. Click to load it

A magical option to fix buggy program. You wish.

Read about short-circuit evaluation here:
http://en.wikipedia.org/wiki/Short-circuit_evaluation

Then fix your bug(s).
Also note that read()ing files one byte at a time is extremely
inefficient. Better read larger chunks, and memcmp()are them.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

Re: tried to compace same file
2007-02-13, 22:51(-08), Paul Pluzhnikov:
[...]
Quoted text here. Click to load it
[...]
Quoted text here. Click to load it
[...]

Or use stdio's getc/fgetc.

--
Stéphane

Re: tried to compace same file
 actually I was trieng to write a program  like diff  where if line is
different it should be printed with line no. I first tried with same
file only. How can I use memcmp or some other API . I am not getting
please tell me

   Regards
    Sumit Shrivastava

wrote:
Quoted text here. Click to load it



Re: tried to compace same file
Hello,

Quoted text here. Click to load it

To make it short: Do your homework, buy a book about C language, read and
understand it.
This is not the right group for these questions.

Regards,
Sebastian



Re: tried to compace same file
Quoted text here. Click to load it

That code segment is not comparing anything. Probably you meant to
write

  while(read(fd1, ptr1, 1) == read(fd2, ptr2, 1))  { ... }

As mentioned in another reply, this is a very inefficient way to
compare two files, but may be acceptable if the files are small.


Roberto Waltman

[ Please reply to the group,
  return address is invalid ]

Re: tried to compace same file
Quoted text here. Click to load it

Indeed, so inefficient as to not work.
The return value from each read() is likely to be 1, 0 or -1.

Peter



Re: tried to compace same file
Quoted text here. Click to load it

@$#&!! I need a coffee transfusion immediately!

Roberto Waltman

[ Please reply to the group,
  return address is invalid ]

Re: tried to compace same file
Quoted text here. Click to load it
Calm down supposedly he's a student who is to lazy to do his homework
himself.

-Michael

Re: tried to compace same file

| Hi,
|   I tried to compare same file on linux. for this i did
|  while(read(fd1, ptr1, 1)  || read(fd2, ptr2, 1))  {

This logic says:  try to read from fd1 and if that fails then try to
read from fd2.  As long as reading from fd1 succeeds, reading from
fd2 will not be tried.  In a loop it will repeat reading from fd1
until EOF makes it fail, then it will repeat trying fd1 over and over
and reading from fd2 on each of those failures.

You need to read both files then you need to compare the contents of
the buffers after reading.

--
|---------------------------------------/----------------------------------|
| Phil Howard KA9WGN (ka9wgn.ham.org)  /  Do not send to the address below |
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline