-3

i wrote a code with c, that reads from 2 files, and than i compare the 2 buffers with strcmp. for some strings, even when i read from the exact same file, strcmp returns !=0

anybody has an idea why?

here is the relevent part from the code:

read_max[0]= read(fdin[0],read_buf,read_offset) ;
read_max[1]=read(fdin[1],read_buf2,read_offset);

    if(strcmp(read_buf,read_buf2)){
        same=1;
    }

i tried to check for answers on this forum and in others, but the only answer i found was for the case of '\n' that is added. as i wrote in the headline, the problem isnwt that it doesn't get inside the if, but that it does( because it doesn't return 0)! its not the case here. the way of writing the strings are the same. in edition, sometimes strcmp works properly.

for example, if the file starts with whitespace or \n, it goes wrong. thank you!

Noam
  • 1
  • 1
  • Return value of `0` is falsey in C. – lurker May 11 '17 at 13:35
  • Please create a [mcve]. – n. m. could be an AI May 11 '17 at 13:37
  • Pare your files down to a single line -- as short as possible -- that produces the error, and add it to your question. See [this](http://stackoverflow.com/help/mcve). – Beta May 11 '17 at 13:37
  • 3
    `read()` doesn't necessarily write a *string* to the destination. You, as the programmer, are responsible to manage that so that you can send the data to `strcmp()`. – pmg May 11 '17 at 13:41
  • What pmg wrote: If you want to read lines from a text file, use `fgets`. The functions `fread` and `read` are for reading a fixed number of raw bytes. – M Oehm May 11 '17 at 13:42
  • i editted my question. i'm not allowed to use something else, only read(). thank you for answering!! – Noam May 11 '17 at 13:46
  • `strcmp()` always works properly. However its behavior depends on the data provided so if you are not seeing an expected behavior then the problem will be with the data you are providing. It could be embedded binary zero characters or a difference in the string lengths or not a terminating binary zero character. Remember that a binary zero is the string terminator in C. You may need to use the `strncmp()` function which takes an additional max character count. Also you need to check that your data is actually `char` text characters and not UTF-16 UNICODE or multi-byte characters. – Richard Chambers May 11 '17 at 13:53
  • Did you check if both `read` operations return the same amount of data? Did they read all the data expected? – Gerhardh May 11 '17 at 13:55
  • 1
    `strcmp` needs `NUL` terminated strings. From your piece of code we cannot see if you ensure the strings you compare are NUL terminated. If they are not `NUL` terminated, the behaviour of `strcmp` is undefined. – Jabberwocky May 11 '17 at 14:14

2 Answers2

6

You should use memcmp instead:

if(read_max[0] != read_max[1] || memcmp(read_buf,read_buf2, read_max[0]) != 0) {
    /* Buffers are different */
}
Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33
0

The function srtcmp() returns 0 when two strings are equal.

Change code like this:

if(!strcmp(read_buf,read_buf2)){
    same=1;
}

or

if(strcmp(read_buf,read_buf2) == 0){
    same=1;
}