ZacCarlson's Profile User Rating: -----

Reputation: -7 Shunned
Group:
Active Members
Active Posts:
148 (0.22 per day)
Joined:
08-October 12
Profile Views:
2,193
Last Active:
User is offline Feb 03 2014 08:36 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   ZacCarlson has not set their status

Posts I've Made

  1. In Topic: Using sprintf with the write system call

    Posted 30 Jan 2014

    I appreciate the psuedo code but looks like a lot of reworking my code. I think I wanna try the lseek. If you wish not to help then no worries, but thanks for the help so far.
  2. In Topic: Using sprintf with the write system call

    Posted 30 Jan 2014

    I kinda understand what you're trying to say but not quite. We're performing a binary diff of files, like cmp does. How would I go about tracking file offsets compared to seeking? I don't understand the diff as lseek() deals with offsets. Elaborate please.

    Or are you suggesting I just adjust my current code to help track file offsets? If so, guide me on how I would look into or go about doing that.
  3. In Topic: Using sprintf with the write system call

    Posted 30 Jan 2014

    I have the code working now. My new problem is a good problem as it makes sense - if byte_read_f1 and byte_read_f2 are different, then the next read operations are at different positions. I was told lseek() is good for that and I can see that and am still in the process of understanding lseek fully but not quite there.

    Where would I adjust the file offsets with lseek()? I'm guessing before the while loop? And then do an if statement with SEEK_CUR to compare if the two files are at different positions and if so then to back track or whatever? Would I have to change my while loop?

    void compare_two_binary_files(int f1, int f2)
    {
    	//write(1, sprintf, 10);
    	ssize_t byte_read_f1, byte_read_f2, length, numRead, bob, length2;
    	char buf1[BUF_SIZE], buf2[BUF_SIZE], a[100], b[100], counter[100];
    	int count = 0, b_pos1, b_pos2;
    	while (((byte_read_f1 = read(f1, buf1, sizeof buf1)) > 0) && ((byte_read_f2 = read(f2, buf2, sizeof buf2)) >0)) {
      		length = byte_read_f1;
    		length2 = byte_read_f2;
    		ssize_t len =  byte_read_f1 <byte_read_f2 ? byte_read_f1 : byte_read_f2;
    		b_pos1 = memcmp(buf1, buf2, len);
    		if (memcmp(buf1, buf2, len) != 0){  // use memcmp for speed
    			ssize_t i;
        			for (i = 0; i<len; i++){
          				if (buf1[i] != buf2[i]){ 
        	 				sprintf(counter, "Byte pos where two files differ is:%d\n", count + i + 1);
        					sprintf(a, "Byte value of file 1: %o\n", buf1[i]);
        					sprintf(b, "Byte value of file 2: %o\n", buf2[i]);
    					write(1, counter, 100);
    					write(1, b, 100);
    					write(1, a, 100);
      				}
        			}
    		}
    		count++;
    	}
    }	
    
    
  4. In Topic: Using sprintf with the write system call

    Posted 30 Jan 2014

    I debugged with printf statements and found the byte length as 1, which is obviously wrong. Guidance on why I'm reading in the wrong byte length of my files?

    Also, looking at memcmp, I found this, and does it mean I can't use "!="?:

    if Return value if < 0 then it indicates str1 is less than str2

    if Return value if > 0 then it indicates str2 is less than str1

    if Return value if = 0 then it indicates str1 is equal to str2
         void compare_two_binary_files(int f1, int f2)
         {
                 ssize_t byte_read_f1, byte_read_f2, length, numRead, bob, length2;
                 char buf1[BUF_SIZE], buf2[BUF_SIZE], a[100], b[100], counter[100];
                 int count = 0, b_pos1, b_pos2;
                 while ((byte_read_f1 = read(f1, buf1, sizeof buf1) > 0) && (byte_read_f2 = read(f2, buf2, sizeof buf2) >0)) {
                         length = byte_read_f1;
                         length2 = byte_read_f2;
                         printf("F1 byte length:%o\n", length);
                         printf("F2 byte length:%o\n", length2);
                         ssize_t len =  byte_read_f1 <byte_read_f2 ? byte_read_f1 : byte_read_f2;
                         b_pos1 = memcmp(buf1, buf2, len);
                         printf("Memcmp: %d\n", b_pos1);
                         if (memcmp(buf1, buf2, len) != 0){  // use memcmp for speed
                                 ssize_t i;
                                 for (i = 0; i<len; i++){
                                         if (buf1[i] != buf2[i]) break;
                                 }
         }
    
    
  5. In Topic: Using sprintf with the write system call

    Posted 30 Jan 2014

    I'm posting new, cleaner code maybe. It's not going into the
    if (memcmp(buf1, buf2, len) != 0){
    
    unless I change != to ==. Then I get wrong numbers. I'm supposed to get byte location 4, file 1 to be 65 and file 2 to be 143, but get pos 1, file 1 is 163 and file 2 is 115:

    void compare_two_binary_files(int f1, int f2)
    {
            ssize_t byte_read_f1, byte_read_f2, length, numRead, bob;
            char buf1[BUF_SIZE], buf2[BUF_SIZE], a[100], b[100], counter[100];
            int count = 0, b_pos1, b_pos2, hey;
            while ((byte_read_f1 = read(f1, buf1, sizeof buf1) > 0) && (byte_read_f2 = read(f2, buf2, sizeof buf2) >0)) {
                    ssize_t len =  byte_read_f1 <byte_read_f2 ? byte_read_f1 : byte_read_f2;
                    if (memcmp(buf1, buf2, len) != 0){
                            ssize_t i;
                            for (i = 0; i<len; i++){
                                    if (buf1[i] != buf2[i]) break;
                            }
                            sprintf(counter, "Byte pos where two files differ is:%lld\n", (long long) count + i);
                            sprintf(a, "Byte value of file 1: %hho\n", buf1[i]);
                            sprintf(b, "Byte value of file 2: %d\n", buf2[i]);
                            write(1, counter, 100);
                            write(1, a, 100);
                            write(1, b, 100);
                            break;
                    }
                    count += len;
            }
    }
    
    

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

ZacCarlson hasn't added any friends yet.

Comments

ZacCarlson has no profile comments yet. Why not say hello?