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)) { .... .... .... }

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

Reply to
sumit
Loading thread data ...

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

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

As it should.

Because the C language standard requires it be so.

A magical option to fix buggy program. You wish.

Read about short-circuit evaluation here:

formatting link

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.
Reply to
Paul Pluzhnikov

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 ]
Reply to
Roberto Waltman

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

Peter

Reply to
Peter Dickerson

| 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 |
| first name lower case at ipal.net   /  spamtrap-2007-02-14-1016@ipal.net |
|------------------------------------/-------------------------------------|
Reply to
phil-news-nospam

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

Roberto Waltman

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

Calm down supposedly he's a student who is to lazy to do his homework himself.

-Michael

Reply to
Michael Schnell
2007-02-13, 22:51(-08), Paul Pluzhnikov: [...]
[...]
[...]

Or use stdio's getc/fgetc.

--
Stéphane
Reply to
Stephane CHAZELAS

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

Reply to
sumit

Hello,

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

Reply to
Sebastian

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.