Thread: offset

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    offset

    How can I find the max offset?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXCHAR 2048    //number of characters to read from disk
    
    int main(void)
    {
        char filename[256];
        char text[256];
        long x;
        char buffer[MAXCHAR+1];
        FILE *f;
        long matches = 0;
        char *a;
    
    
    
        printf("Filename:");    //Get filename
        gets(filename);
       
    
        if((f = fopen(filename,"rb")) == NULL)
        {
            printf("Error opening %s\n",filename);
            exit(0);
        }
                                //Get search string
        printf("Hunt for text:");  //Edit this to look for MC FFD9 03B
        gets(text);
    
        printf("\nLooking in %s for \"%s\" . . .\n",filename,text);
    
        printf(">> %s found\n",filename);
        printf(">> Hunting for \"%s\"\n",text);
    
    /* Scan for a matching byte */
    
        x = 1;                  //File offset
    
        while(fgets(buffer,MAXCHAR,f))
        {
            if( (a = strstr(buffer,text)) != NULL)
            {
                matches++;      //inc. match count
                printf("Found at offset %Lu\n",x+a-buffer);
        
            }
            x+=strlen(buffer);      //adjust offset
        }
    
        fclose(f);
    }

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    On a side note, don't use gets() use fgets() instead the reason behind this is explained in the FAQ:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    Search

    I need to search a file for a certain characters and report were it is in the file. I need too test if the characers are the ones just before the EOF or not.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    Best solution I can come up with is too strrchr then copy the what remains of the text file. If only the strch charecter is left then it is just before the EOF. Anyone have a better method?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm not sure I understand your question, but here goes:
    Code:
    /* Scan for a matching byte */
    
        offset = -1;
        x = 1;                  //File offset
    
        while(fgets(buffer,MAXCHAR,f))
        {
            if( (a = strstr(buffer,text)) != NULL)
            {
                matches++;      //inc. match count
                printf("Found at offset %Lu\n",x+a-buffer);
                offset = x + a - buffer;
        
            }
            x+=strlen(buffer);      //adjust offset
        }
        if (offset != -1)
           printf("last match found at offset: %d\n",offset);
        printf("end of file at offset: %d\n",x);

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The best I've been able to string this together, since Rhidian has spread questions and various fragments from replies around various threads and forums, is like this:
    Search through a file (likely a JPEG) opened in binary mode and search for this format's end-of-file indicator, which happens to be a hex value such as FFD9.
    A quick hack, since I'm not exactly sure that this is what Rhidian is asking, might be something like this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       static const char filename[] = "Ocean Wave.jpg";
       FILE *file = fopen(filename, "rb");
       if ( file != NULL )
       {
          unsigned char *buffer;
          long i, size;
    
          fseek(file, 0L, SEEK_END);
          size = ftell(file);
          rewind(file);
    
          buffer = malloc(size);
          if ( buffer != NULL )
          {
             static const unsigned char jpeg_eof[] = {0xFF,0xD9};
             if ( fread(buffer, sizeof *buffer, size, file) == size )
             {
                for ( i = 0; i < size - 1; ++i )
                {
                   if ( buffer[i] == jpeg_eof[0] && buffer[i + 1] == jpeg_eof[1] )
                   {
                      printf("i = %lX, size = %lX\n", i, size);
                      break;
                   }
                }
             }
          }
          free(buffer);
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
    i = 121B7, size = 121B9
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Judging from the comments in Rhidian's code, that looks like what he's after.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsigned Long returning signed value
    By dinklebaga in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 06:07 AM
  2. My memory management solution
    By cboard_member in forum Game Programming
    Replies: 20
    Last Post: 08-23-2006, 09:07 AM
  3. Reading in a binary file from offset into another file
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2006, 03:01 AM
  4. Relocation in .obj -files
    By willkoh in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2005, 01:59 PM
  5. I still do not understand offset, please give me examples
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2002, 09:01 AM