Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

question about structures

TheCreeperTheCreeper Member Posts: 55
hi,

i would like to read a sector from a disk into an array and then convert the first 10 or so bytes into a structure. can i do this? or do i have to convert each member individually? any help is nice.

Comments

  • DariusDarius Member Posts: 1,666
    : hi,
    :
    : i would like to read a sector from a disk into an array and then convert the first 10 or so bytes into a structure. can i do this? or do i have to convert each member individually? any help is nice.
    :

    I'll assume you can handle getting the sector, otherwise you'll need to specify which OS you are using and you'll need administrator privileges in that OS. There obviously is no portable way to do this.

    Once you do that, you can cast the block of memory that you read the data into to a pointer of the type you want.

    One thing you may need to watch out for is the compiler may not densely pack the structure items. You may need to specify (via a #pragma or a commandline switch) that the compiler NOT pad the structure.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • TheCreeperTheCreeper Member Posts: 55
    : : hi,
    : :
    : : i would like to read a sector from a disk into an array and then convert the first 10 or so bytes into a structure. can i do this? or do i have to convert each member individually? any help is nice.
    : :
    :
    : I'll assume you can handle getting the sector, otherwise you'll need to specify which OS you are using and you'll need administrator privileges in that OS. There obviously is no portable way to do this.
    :
    : Once you do that, you can cast the block of memory that you read the data into to a pointer of the type you want.
    :
    : One thing you may need to watch out for is the compiler may not densely pack the structure items. You may need to specify (via a #pragma or a commandline switch) that the compiler NOT pad the structure.
    :
    : "We can't do nothing and think someone else will make it right."
    : -Kyoto Now, Bad Religion
    :
    :


    since i tried that before posting and since i probally attempted to do it wrong, im just gonna post some of my code because im not sure about how to go about doing it.

    [code]
    /*the data i want from the disk*/
    typedef struct
    {
    char name[10];
    int id;
    int some_data;
    }MyData;

    void ReadData(void)
    {
    MyData data;
    char buffer[514];
    /*read a sector into buffer here, all 512 bytes*/
    /*
    *****QUESTION*******
    1) Should i make variable 'data' a pointer?
    2) Should i allocate my buffer with malloc?
    3) Exactly how do I convert my buffer into my data
    */
    return;
    }

    [/code]
  • DariusDarius Member Posts: 1,666
    [b][red]This message was edited by Darius at 2002-9-27 23:21:10[/red][/b][hr]
    : : : hi,
    : : :
    : : : i would like to read a sector from a disk into an array and then convert the first 10 or so bytes into a structure. can i do this? or do i have to convert each member individually? any help is nice.
    : : :
    : :
    : : I'll assume you can handle getting the sector, otherwise you'll need to specify which OS you are using and you'll need administrator privileges in that OS. There obviously is no portable way to do this.
    : :
    : : Once you do that, you can cast the block of memory that you read the data into to a pointer of the type you want.
    : :
    : : One thing you may need to watch out for is the compiler may not densely pack the structure items. You may need to specify (via a #pragma or a commandline switch) that the compiler NOT pad the structure.
    : :
    : : "We can't do nothing and think someone else will make it right."
    : : -Kyoto Now, Bad Religion
    : :
    : :
    :
    :
    : since i tried that before posting and since i probally attempted to do it wrong, im just gonna post some of my code because im not sure about how to go about doing it.
    :
    : [code]
    : /*the data i want from the disk*/
    : typedef struct
    : {
    : char name[10];
    : int id;
    : int some_data;
    : }MyData;
    :
    : void ReadData(void)
    : {
    : MyData *data;
    : char buffer[514];
    sector_reader(buffer); //whatever that reads the data in
    data=(MyData *)buffer;
    //do whatever with data here
    //Note that it will be destroyed when you return if you need to
    //keep it then you'll need to malloc the buffer.
    : return;
    : }
    :
    : [/code]
    :


    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion



  • DB1DB1 Member Posts: 1,142
    : hi,
    :
    : i would like to read a sector from a disk into an array and then convert the first 10 or so bytes into a structure. can i do this? or do i have to convert each member individually? any help is nice.
    :


    http://codeguru.earthweb.com/system/ReadSector.html
  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    ______ || http://forcoder.org || free video tutorials and ebooks about [ C Objective-C Assembly C# Visual Basic .NET MATLAB Go PHP R Delphi Scratch C++ Swift Java Visual Basic Perl Ruby PL/SQL Python JavaScript Rust Fortran Apex Bash D Hack Scheme Scala Julia Erlang Lisp Dart Logo VBScript Prolog LabVIEW Awk ABAP FoxPro F# Ada SAS Clojure ML Alice Crystal Transact-SQL COBOL Lua Kotlin ] ________

Sign In or Register to comment.