accessing bytes of a file

This is a discussion on accessing bytes of a file within the C++ Programming forums, part of the General Programming Boards category; I have a file opened and in a loop I can access each byte seperately . How would I go ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    accessing bytes of a file

    I have a file opened and in a loop I can access each byte seperately. How would I go about accessing advanced bytes in a file?
    example:

    while ((byte=fgetc(myfile)) != EOF) {
    printf("%c ",byte);
    }

    Inside the loop, how do I access (byte+1). Actually using byte+1 gives me the next ascii code in front of byte(no good). Help anyone?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One option is:

    while ((byte=fgetc(myfile)) != EOF) {
    printf("%c ",byte);
    next_byte=fgetc(myfile);
    printf("%c ",next_byte);
    if (next_byte == EOF)
    break;
    }

    This would read two bytes each time thru the loop.
    If you need to put this byte back, use ungetc().

    while ((byte=fgetc(myfile)) != EOF) {
    printf("%c ",byte);
    next_byte=fgetc(myfile);
    printf("%c ",next_byte);
    ungetc(next_byte,myfile)
    }

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    or
    Code:
    int handle=open(path,O_BINARY,S_IREAD);
    long size=filelength(handle);
    int buffersize=size;
    if (size>64000) buffersize=64000;
    
    unsigned char *buffer=new unsigned char[64000];
    
    //If file is larger than 64000 bytes, use the 64000 byte buffer to
    //cache new info into it by doing another read
    
    read(handle,&buffer,buffersize);
    int count=0;
    for (int i=0;i<buffersize;i++)
    {
       printf("%X ",buffer[i]);
       count+=3;
       if (count>=80) 
       {
         count=0;
         printf("\n");
       }
    }
    Then when the user browses past the buffersize, just do another block read and fill the buffer with data. You can either fill it with all new data or you can scrap the first line and add new data to the last line allowing the user to scroll down the bytes.

    I've not tested this code, but it will browse the bytes of a file.
    It's not straight C++, so if you want to you can use streams instead of this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithm for duplicate file checking help
    By geekoftheweek in forum C Programming
    Replies: 1
    Last Post: 04-04-2009, 02:46 PM
  2. Reverse Engineering on a Download file
    By c_geek in forum C Programming
    Replies: 1
    Last Post: 03-22-2008, 04:15 PM
  3. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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