Alexey
Alexey

Reputation: 221

how compare char * array if keep it in stack in C

char *file1charbuf=(char*)malloc(sizeof(char));
char *file2charbuf=(char*)malloc(sizeof(char));

in loop until EOF I read char into *file1charbuf and *file2charbuf and then compare.

...
check=read(file1, file1charbuf, 1);
check2=read(file2, file2charbuf, 1);
if (*file1charbuf!=*file2charbuf){
         printf("differ: char %i, line %i\n",charpos,linepos);
         exit(1);
}
....

compare works fine but i want to keep pointer in stack,not in heap. Also malloc is C lib function.

char *file1charbuf[1]; //1 element array of char
char *file2charbuf[1];

with that comparing doesnt work right

...
if (file1charbuf[0]!=file2charbuf[0]){
         printf("differ: char %i, line %i\n",charpos,linepos);
         exit(1);
}
...

and the second question. Is it necessary close(file1) if exit(1) found?

Upvotes: 0

Views: 237

Answers (5)

Lefteris E
Lefteris E

Reputation: 2814

Code to compare 2 files and list the line and character that differ. It doesn't compare them line-by-line rather character by character, but keeps track of the line number and loads the file into blocks of 4Kbytes.

#define BLOCKSIZE 4096

char file1charbuf[BLOCKSIZE]; //4096 character buffer in stack
char file2charbuf[BLOCKSIZE];
int linepos=1, charpos =1, i, b1, b2 ;
int file1 = open("1.txt", O_RDONLY);
int file2 = open("2.txt", O_RDONLY);

do{
   b1=read(file1, file1charbuf, BLOCKSIZE);
   b2=read(file2, file2charbuf, BLOCKSIZE);
   for(i=0; i < b1 && i < b2; ++i){
      if (file1charbuf[i]!=file2charbuf[i]){
         printf("differ: char %i, line %i\n",charpos,linepos);
         close(file1);
         close(file2);
         exit(1);
      }
      if (file1charbuf[i] == '\n'){
         ++linepos;
         charpos=0;
      }
      ++charpos;
   }
}while(   (b1 == BLOCKSIZE || (file1==STDIN && file1charbuf[b1-1] != 0x26)) 
       && (b2 == BLOCKSIZE || (file2==STDIN && file2charbuf[b2-1] != 0x26)) );

if (b1 != b2)
   printf("One bigger than the other\n");
close(file1);
close(file2);

About reading from STDIN:

read() from stdin unblocks when you press enter. At that time only the characters you typed so far are availiable to be read. This means that b1 will equal to the characters you typed so far. The do-while condition will fail because read didn't read all the 4096 bytes it requested, which will make it think that it's because the stream has ended. To continue reading after the \n we have to change the while condition so that if the file descriptor we are reading is STDIN, to continue reading unless the last character we read is the stream end character which i think is 0x26. I hope that ^D will also unblock read() in the same way enter does.

Upvotes: 0

Leonid Volnitsky
Leonid Volnitsky

Reputation: 9144

...
char c1, c2;
check=read(file1, &c1, 1);
check2=read(file2, &c2, 1);
if (c1!=c2) {
...

Upvotes: 0

unwind
unwind

Reputation: 399959

There's quite a few issues with the code you're showing, I'm afraid:

  1. Don't cast the return value of malloc(), in C.
  2. sizeof (char) is always 1 in C, so it's quite pointless to use it like this.
  3. You're declaring arrays of pointers to characters, when you seem to mean arrays of characters.

    char *file1charbuf[1]; //1 element array of char should be
    char file1charbuf[1]; /* 1-element array of char */ or, of course, just
    char file1charbuf; /* 1-character buffer. */

  4. You're doing read() with 1-byte buffers, which is fantastically inefficient.

  5. To compare character arrays (not necessarily strings, i.e. doesn't have to be zero-terminated) with more than 1 character, use memcmp().
  6. It's best to call close() on all open file descriptors before calling exit(). On most operating systems, the death of the process will lead to all its resources being re-claimed, but it's better to do it explicitly. If the file I/O was using the buffered FILE*-based calls, there would be no need since those are closed by exit() automatically in all environments.

Upvotes: 5

suspectus
suspectus

Reputation: 17288

The definitions are not pointers to char arrays, they are char arrays.

char file1charbuf[1]; //1 element array of char
char file2charbuf[1];

There is no difference in comparision semantics of strings using memory allocated on the heap and that of memory allocated on the stack.

Upvotes: 0

James M
James M

Reputation: 16718

char *file1charbuf[1]; is an array of one pointer to a char. If you want an array of one char, use char file1charbuf[1].

Upvotes: 0

Related Questions