2

C newbie here.

Banging my head against the wall with this one...:/ I'm trying to compare to files which are not been used by any other process which means that they are static, using only system calls. I have no problems doing so using fopen() but it feels much more complicated when using just open(), read() and write()...

here's what I got so far:

...//Some code here to get file descriptors and other file manipulation checks
int one = read(srcfd1,buf1,src1_size); 
int two = read(srcfd2,buf2,src2_size);
printf("%s\n",buf1);  //works fine till it gets here...
int samefile = strcmp(buf1,buf2);  //Crashes somewhere around here..
if (samefile != 0)
{
    printf("not equle\n");
    return(1);
}
else 
{
    printf("equle\n");      
    return(2);
}

So basically, what I think I need to do is to compare the 2 buffers but this is not seem to be working...

I found something which I believe should give me some idea here but I can't make sense of it (the last answer in the link...).

The return values are irrelevant .

Appreciate any help I can get...:/

Community
  • 1
  • 1
Moran
  • 35
  • 5

2 Answers2

2

Your buffers are not NUL terminated, so it doesn't make sense to use strcmp - this will almost certainly fail unless your buffers happen to contain a 0 somewhere. Also you don't say whether these files are text files or binary files, but to make this work (for either text or binary) you should change:

int samefile = strcmp(buf1,buf2);  //Crashes somewhere around here..

to:

int samefile = memcmp(buf1,buf2,src1_size); // use memcmp to compare buffers

Note that you should also check that src1_size == src2_size prior to calling memcmp.

Paul R
  • 208,748
  • 37
  • 389
  • 560
0

This crashes since the buffers possibly are not null terminated. You are trying to print them as string "%s" in printf and doing a strcmp too.

You can trying null terminating the buffers, after your read calls, and then print them as string.

buf1[one] = '\0';
buf2[two] ='\0';

This will most likely fix your code. But a few other points,

1) Are your buffers sufficiently large as the file?
2) Better to partially read data, than to try to grab everything in one go.
(means use a loop to read data, till it returns a 0) 
like, 
    Assuming the array "buf" is sufficiently large to hold all the file's data.
    The number "512" means,  read will at most try to read 512 bytes and the
    iteration will continue, till read returns 0 (when there is no more data) or 
    may be a negative number, in case of any error. The array's index is getting 
    incremented, by the number of bytes read till now, so that the data does not 
    get overwritten.
    An example - If a file is having say 515 bytes, read will be called thrice. 
    During  the first call it will return 512, for the 2nd call it will return 3
    and the third call will return 0. (read call returns the number of bytes,
    actually  read)   

    index = 0;
    while(  (no_of_bytes_read = read(fd, buf + index, 512)) > 0)
    {
         index = index  +  no_of_bytes_read;

    }
  • Tanmoy, This is interesting - Can you elaborate on how to partially read the data - I was trying this as some point but couldn't make it to work. I think I'm missing some basic understanding on how read() works "behind the scenes"... – Moran Mar 25 '14 at 18:14
  • @user3037474, I have added some more explanation for read in my anaswer. Hope this helps. – Tanmoy Bandyopadhyay Mar 25 '14 at 18:58