C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-04-2008, 11:46 AM   #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
ChaoticMachiner is offline   Reply With Quote
Old 05-04-2008, 12:36 PM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
make your value - you are trying to print with %x format - unsigned char
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 05-04-2008, 01:19 PM   #3
Registered User
 
Join Date: Apr 2008
Posts: 278
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.
root4 is offline   Reply With Quote
Old 05-04-2008, 03:40 PM   #4
Registered User
 
Join Date: May 2008
Posts: 2
Thank you for helping out a C newbie. That fixed it.

CM
ChaoticMachiner is offline   Reply With Quote
Reply

Tags
printf formats

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:08 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22