Thread: C++ - Get current position of file pointer.

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    56

    Question C++ - Get current position of file pointer.

    Hello,

    I'm trying to get the file pointer position.
    The result I'm getting is 4096 when I know its less.
    I understand that the 4096 is probably the cluster size.
    I know I can count the read sizes, just seems a bit clumsy,
    How do I get the absolute position of the file pointer
    when I've finished my reads, not always to the strucure end?

    Regards
    Code:
        myStructure pt2[1];
        
        FILE * in2 = fopen(tmp.c_str(), "r");
    
        fseek(in2, 0x00, SEEK_SET); // go to start
        fread(pt2, sizeof(myStructure), 1, in2); //structure
        
        ...various reads // all OK
        
        long size = ftell (in2);
        printf("pos - %lx - %u\n", size, size); // prints "pos - 1000 - 4096"
    
        fpos_t pos;
        fgetpos (in2, &pos);
        printf("pos - %lx\n", pos);  // prints "pos - 1000"

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What OS?
    Are you compiling with full warnings? On gcc use -Wall -W -pedantic.

    What does your structure look like?
    How do you know that the position isn't 4096?
    Are you taking account of padding in your calculations?

    You aren't using the correct format to print a long. It should be "%lu".

    An fpos_t is not necessarily a long or even an integer (it could be a struct or array). It's an opaque object and not meant to necessarily be printable at all.

    You probably need to post a complete runnable program to get help.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    So how'd that work out for you?

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello algorism,

    Windows 10 - 32 bit
    gcc version 5.3.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)

    Compiling from within Qt Creator 4.2.0

    *.pro file
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    CONFIG += C++11

    SOURCES += test.cpp

    Thanks for your reply.
    I've corrected the printf's, getting the same results.
    No warnings or errors from the compiler
    I'm only reading about 60 bytes into the structure.
    If you still want compileable code, I can do this tomorrow, Please let me know.
    I'm getting as far as shown OK, now I want to save the pointer for later use,
    I need to get its postion.

    Regards
    Code:
        long size = ftell (in2);
        printf("pos - %Ix - %lu\n", size, size);
    
        fpos_t pos;
        fgetpos (in2, &pos);
        printf("pos %I64d\n", pos);
    Code:
    typedef struct {
        unsigned char jmpCode[3];                 // 3
        char           oem[8];                    // 8
        unsigned short sector_size;               // 2
        unsigned char  sectors_per_cluster;       // 1
        unsigned short reserved_sectors;          // 2
        unsigned short root_dir_entries;          // 2
        unsigned short total_sectors_short;       // 2 
        unsigned char  media_descriptor;          // 1
        unsigned short fat_size_sectors;          // 2
        unsigned short sectors_per_track;         // 2
        unsigned short number_of_heads;           // 2
        unsigned long  hidden_sectors;            // 4
        unsigned long  total_sectors_long;        // 4
        unsigned long  sectors_per_fat;           // 4
        unsigned short flags;                     // 2
        unsigned short fat32_version;             // 2
        unsigned long  root_directory_cluster_no; // 4
        unsigned short file_info;                 // 2
        unsigned short backup_root;               // 2
        char           reserved[12];              // 12
        unsigned char  drive_number;              // 1
        unsigned char  unused;                    // 1
        char           extended_sig;              // 1
        unsigned long  serial;                    // 4
        
    //*** I'm only reading this far - about 60 bytes ***
        
        char           volume_label[11];          // 11
        char           fat_name[8];               // 8
        char           boot_code[448];            // 448
        unsigned short boot_sector_signature;     // 2
    } __attribute((packed)) myStructure;
    Last edited by mad-hatter; 02-14-2017 at 03:05 PM.

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    56
    fpos_t

    Object containing information to specify a position within a file
    This type of object is used to specify a position within a file. An object of this type is capable of specifying uniquely any position within a file.
    The information in fpos_t objects is usually filled by a call to fgetpos, which takes a pointer to an object of this type as argument.
    The content of an fpos_t object is not meant to be read directly, but only to be used as an argument in a call to fsetpos.

    *************

    It looks like I can save the position without knowing the actual position.
    Thanks for your time.

    Regards

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your original code shows that you forgot to open the file in binary mode (rb).

    You say you only read up to a certain point in the struct, but your original code has an fread that reads an entire struct.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    A better question, to begin with, might be why are you using C file I/O functions in C++?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello.

    algorism

    Opening (rb) makes a lot of difference (my mistake).
    fread whole structure (again my mistake), I was printing upto a
    certain point in the structure.

    Elkvis
    I'm opening a USB device as a file, don't know how to do this in C++.

    Regards

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Are you familiar with C++ iostreams? Here is a brief example, in the context of your problem:

    Code:
    std::ifstream infile("/dev/usb/whatever", std::ios::binary); // open the file
    infile.seekg(0); // reset to beginning of file
    infile.read(pt2, sizeof(myStructure)); // read the structure
    auto position = infile.tellg(); // get the current position
    For further reading on the subject, look up a C++ binary file I/O tutorial on google.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello,

    Elkvis
    Are you familiar with C++ iostreams? Here is a brief example, in the context of your problem:
    Not yet, but playing. Thanks.

    Regards
    Code:
    int main() 
    {
        int count = 0;
    
        Fat32BootRecord pt2;
    
        char buff[sizeof(Fat32BootRecord) + 1]; 
        
        std::vector <int> posList = { (sizeof(pt2.jmpCode)), (sizeof(pt2.oem)) };
                             // and the rest of structure
        
        std::vector <string> sType = { "Jmpcode", "OEM" };
                             // and the rest of structure 
        
        std::stringdrLetter = "J:"; // GET THE RIGHT DEVICE    
        std::stringdevice = "\\\\.\\" + drLetter; 
        
        std::ifstream infile(device, std::ios::in | std::ios::binary);
        
        if (!infile)
            {
                 cerr << "Device '" + drLetter + "' could not be opened!" << endl;
                 exit(-1);
            }
    
        infile.seekg(0); // reset to beginning of file
        infile.read(buff, sizeof(Fat32BootRecord)); // read the structure 
    
        for (int j = 0; j < posList.size(); j++)
        {
            printf("%s - %i\n", sType[j].c_str(), posList[j]);
            for(int i = count; i <  posList[j] + count; i++) 
            {
                printf("%i - %c - %d - %x\n",
                     i, buff[i], buff[i], buff[i]);
            }
            count = count + posList[j];
        } 
        exit(0);
    }
    Code:
    Jmp code - 3
         0 -  - -21 - ffffffeb
         1 - X - 88 - 58
         2 -  - -112 - ffffff90
     OEM - 8
         3 - M - 77 - 4d
         4 - S - 83 - 53
         5 - D - 68 - 44
         6 - O - 79 - 4f
         7 - S - 83 - 53
         8 - 5 - 53 - 35
         9 - . - 46 - 2e
         10 - 0 - 48 - 30
    Last edited by mad-hatter; 02-16-2017 at 04:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. current window position
    By atique in forum Windows Programming
    Replies: 2
    Last Post: 08-02-2012, 12:58 PM
  2. setting a file position pointer
    By JayCee++ in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2011, 06:40 AM
  3. Get current position of pointer
    By Ducky in forum C Programming
    Replies: 13
    Last Post: 06-19-2010, 11:11 AM
  4. How to get current cursor position?
    By developersubham in forum C Programming
    Replies: 0
    Last Post: 01-14-2007, 01:12 AM
  5. Position FILE pointer to a specified line of Text file
    By jessica_xie in forum C Programming
    Replies: 2
    Last Post: 02-04-2005, 03:52 PM

Tags for this Thread