Thread: Weird read input or bad printf output?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Weird read input or bad printf output?

    Hi,

    I have been struggling with this now for a couple weeks. I am writing a program that looks for headers of files. Upon finding one, it looks in the header for how large the program is. The code below is a subset of the program I am writing. I already know where the header is, but when I try to read in the bytes I get some weird output.

    The output I get is (Notice the Hex:FFFFFF80):
    Starting File Read -- findenddatorg
    Hex:00 Hex:FFFFFF80 Hex:00 Hex:00 Filename: (null) Startpos: 26215452 Endpos: 0 Size: 0 -- Filelength: 140736547508496

    It should be:
    Starting File Read -- findenddatorg
    Hex:00 Hex:80 Hex:00 Hex:00 Filename: (null) Startpos: 26215452 Endpos: 0 Size: 0 -- Filelength: 140736547508496

    Why in the world do I have a FFFFFF80? If I adjust this line:
    startpos = startpos+28;
    to
    startpos = startpos+29;

    I get:
    Starting File Read -- findenddatorg
    Hex:FFFFFF80 Hex:00 Hex:00 Hex:00 Filename: (null) Startpos: 26215453 Endpos: 0 Size: 0 -- Filelength: 140736975497536

    If I change to: startpos = startpos+30;
    Starting File Read -- findenddatorg
    Hex:00 Hex:00 Hex:00 Hex:40 Filename: (null) Startpos: 26215454 Endpos: 0 Size: 0 -- Filelength: 140734021634096

    Any ideas??

    Below is my code:

    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <inttypes.h>
    #include <errno.h>
    #include <string.h>
    #include <unistd.h>
    
    // My headers
    #include "main.h"
         
    
    char *filename = NULL;
    char *programname = NULL;
    int fd; //File
    FILE *logfile;
    FILE *foundfile;
    int row,col; // Curses row and col
    char *configfilename = NULL;
    char *logfilename = NULL;
    
    
    
    off_t findenddatorg(off_t startpos, off_t endpos, unsigned long long size) {
        //off_t getsize,getsizefinal = 0;
        off_t getsizefinal = 0;
        char getsize[] = "AAAA";
        char getsize2[] ="AAAA";
        char blowme[]="AAAA";
        int *getsize_ptr;
        char out[12];
        int fileend = 0;
        long fileend2,testlong = 0;
        int out_file = 0;
        int read_size = 0;
        int dat_length = 0;
        off_t currpos;
        int hexcnt = 0;
        unsigned long long filelength = 0;
        unsigned long long fileleft = 0;
        char holda,holdb;
    
        startpos = startpos+28;
        currpos = lseek(fd, startpos, SEEK_SET);
        getsize_ptr = &getsize;
    
        if (currpos == -1)
               fprintf(stderr, "Could not seek ending position of --  Startpos: %lld -- Endpos: %lld -- Size: %lld -- Filename: %s\n", startpos,endpos,size,filename);
            else {
                    printf("Starting File Read -- findenddatorg\n");
    
                    read_size = read(fd, getsize, sizeof(getsize));
    		if ( read_size < 0 ) {
    			exit(8);
    		}
    		for (hexcnt = 0; hexcnt < 4; hexcnt++) 
    	    		printf("Hex:%02X ", getsize[hexcnt]);
    
                    printf("Filename: %s \t Startpos: %lld \t Endpos: %lld \t Size: %lld -- Filelength: %lld \n\n",filename, startpos,endpos,size, getsize);
                    return(getsizefinal);
        }
    }
    
    
    
    
    
    
    
    
    int main (int argc, char **argv) {
    
    
     off_t currpos;
     long saveline;
    
        fd = open("test_small.dd", O_RDONLY|O_LARGEFILE);
        //fd = open(filename, O_RDONLY|O_BINARY|O_LARGEFILE);
        if (fd == -1) {
    	fprintf(stderr, "Error opening file: %s\n", argv[optind]);
    	exit(1);
        }
        
        currpos = lseek(fd, 0, SEEK_CUR);
    
    
        saveline = findenddatorg(26215424, 0, 0);
    
    
           return 0;
    }
    Thanks,
    CM

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    make your value - you are trying to print with &#37;x format - unsigned char
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Declare getsize[] as an array of unsigned char as suggested in the previous post.
    The unwanted F come from the propagation of the sign bit when converting to
    an 4 bytes value.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    2
    Thank you for helping out a C newbie. That fixed it.

    CM

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  2. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  4. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM

Tags for this Thread