Thread: need a little help for my assignment

  1. #1
    neversell
    Guest

    need a little help for my assignment

    here is my code:
    /* ===== Part1Main.c ===== */

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include "SharedDisk.h"
    #include "FDisk.h"

    int main()
    {
    Disk *theDisk;
    char buffer;

    if ( DiskOpen( theDisk ) == 0 )
    {
    printf( "Open file failed.\n" );
    exit( 0 );
    }
    else
    {
    printf( "Open file successful.\n" );
    fflush( stdout );
    }
    DumpSector ( buffer );
    DiskClose( theDisk );
    return 0;
    }

    /* ===== FDisk.c ===== */

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <ctype.h>
    #include "SharedDisk.h"
    #include "FDisk.h"


    int DiskOpen( Disk *theDisk )
    {
    int i, j;
    int fd;
    int len;
    char buffer[SIZE];

    fd = open ("disk.img", O_RDONLY );

    len = read ( fd, buffer, 512 );
    if ( len == -1 )
    {
    printf("Unable to read file.\n");
    }
    printf( "%c\n", buffer[ 3 ] );

    return fd;
    }


    void DiskClose( Disk *theDisk )
    {
    int fd;

    close( fd );
    }


    void DumpSector ( unsigned char *buffer )
    {
    int i, j;


    for( i = 0; i < SIZE; i += UPPERSIZE )
    {
    printf( "0x%04x : ", i );
    for( j = i; j <= i + UPPERSIZE; j++ )
    {
    printf( "%02x ", buffer[ j ] );
    fflush( stdout );
    }
    printf( "\t" );

    for( j = i; j <= i + UPPERSIZE; j++ )
    {
    if ( isprint( buffer[ j ] ) )
    printf( "%c", buffer[ j ] );
    else
    printf( ".");
    fflush( stdout );
    }
    printf("\n");
    }
    }


    /* ===== FDisk.h ===== */

    #ifndef __FDISk__
    #define __FDISK__

    #define SIZE 512
    #define UPPERSIZE 15

    #include "SharedDisk.h"

    int DiskOpen( Disk *theDisk );

    void DiskClose( Disk *theDisk );

    void DumpSector( unsigned char *buffer );

    #endif

    /* ===== SharedDisk.h ===== */

    #ifndef __SHAREDISK__
    #define __SHAREDISK__


    typedef struct
    {
    char *Jump;
    char *Manufacturer;
    char *BytesPerSector;
    char *SectorsPerCluster;
    char *ReservedSectors;
    char *FATs;
    char *RootDirectoryEntries;
    char *LogicalSectors;
    char *MediaDescriptorByte;
    char *SectorsPerFAT;
    char *SectorsPerTrack;
    char *Surfaces;
    char *HiddenSectors;
    } Disk;

    #endif

    i am new to c. so i donot really know.

    my question is how do i pass buffer in DiskOpen to DumpSector ( unsigned char *buffer )?

    how do i initialize members of Disk

    how do i close file?
    i do not know how to pass fd in DiskOpen to DiskClose.

    thx for ur help

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >my question is how do i pass buffer in DiskOpen to DumpSector ( unsigned char *buffer )?
    Code:
    DumpSector(&buffer);
    Since, DumpSector() expects a pointer to a variable and not the value itself.

    >how do i initialize members of Disk
    Disk is not an instance, it's a datatype. You need to initialize it's Instance. 'theDisk' is a pointer to a type Disk, you need to allocate memory to this before you can use this, so:
    Code:
    theDisk = malloc(sizeof(Disk));
    if(theDisk == NULL) {
            perror(" malloc");
            exit(-1);
    }
    To allocate memory to it's members:
    Code:
    theDisk->Jump         = malloc(20);
    if(theDisk->Jump == NULL) {
            perror(" malloc");
            exit(-1);
    }
    theDisk->Manufacturer = malloc(20);
    if(theDisk->Manufacturer == NULL) {
            perror(" malloc");
            exit(-1);
    }
    /* ... */
    
    strcpy(theDisk->Jump,         "Here");
    strcpy(theDisk->Manufacturer, "ABC");
    
    printf(" %s %s\n",  theDisk->Jump, theDisk->Manufacturer);
    
    free(theDisk->Jump);
    free(theDisk->Manufacturer);
    free(theDisk);
    >how do i close file?
    How did you open it? If you opened the file with fopen(), use fclose() and if you opened with open(), use close() to close the file.

    >i do not know how to pass fd in DiskOpen to DiskClose.
    Add another parameter to the function DiskClose()
    Code:
    DiskClose(Disk *theDisk, int fd)
    and from DiskOpen()
    Code:
    DiskOpen(theDisk, fd);
    Also, check your code for error trapping, you haven't done any:
    Code:
    fd = open ("disk.img", O_RDONLY );
    if(fd == -1) {
             perror(" open");
             exit(-1);
    }
    
    You haven't initialised 'buffer'
    Last edited by shaik786; 06-29-2002 at 04:03 AM.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    thx for the help



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM