⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hard.c

📁 在写文件系统时
💻 C
字号:
#include <WINDOWS.H>    
#include <WINIOCTL.H>    
#include <STDIO.H>    
   
#include "fat.h"    
   
extern  fat_get_dir_file_list(Byte);   
   
U8 gl_buffer[512];   
BOOLEAN reserved_disk_space;   
   
#define __FLASH    
   
   
#ifdef __FLASH    
   
HANDLE hDevice;               // handle to the drive to be examined     
U8 *rbuf,*rp,*wbuf,*wp;   
U32 rpSector,wpSector;   
   
BOOLEAN Hard_read_open(U32 sector)   
{   
    BOOLEAN bResult;                 // results flag    
    U32 nBytesRead, offset;   
   
    rpSector=sector;   
    offset=512*sector;   
    SetFilePointer(hDevice, offset, 0, FILE_BEGIN);   
   
    // Attempt a synchronous read operation.    
    bResult = ReadFile(hDevice,    
                   rbuf,    
                   512,    
                   &nBytesRead,    
                   NULL) ;    
   
    if(nBytesRead!=512){   
        printf("Read Error!\n");   
        return FALSE;   
    }   
    else{   
        printf("%x ",sector);   
        ;   
    }   
    rp=rbuf;   
    return TRUE;   
}   
   
void Hard_read_close()   
{   
}   
   
U8 Hard_read_byte()   
{   
    if(rp-rbuf==0x200){   
        Hard_read_open(rpSector+1);   
    }   
    return *rp++;   
}   
   
BOOLEAN Hard_write_open(Uint32 sector)   
{   
    BOOLEAN bResult;                 // results flag    
    U32 nBytesRead, offset;   
   
    wpSector=sector;   
    offset=512*sector;   
    SetFilePointer(hDevice, offset, 0, FILE_BEGIN);   
   
    // Attempt a synchronous read operation.    
    bResult = ReadFile(hDevice,    
                   wbuf,    
                   512,    
                   &nBytesRead,    
                   NULL) ;    
   
    if(nBytesRead!=512){   
        printf("Read Error!\n");   
        return FALSE;   
    }   
    else{   
        printf("%x ",sector);     
    }   
    wp=wbuf;   
    return TRUE;   
   
}   
   
void Hard_write_close()   
{   
    BOOLEAN bResult;                 // results flag    
    U32 nBytesRead, offset;   
   
    offset=512*wpSector;   
    SetFilePointer(hDevice, offset, 0, FILE_BEGIN);   
   
    // Attempt a synchronous write operation.    
    bResult = WriteFile(hDevice,    
                   wbuf,    
                   512,    
                   &nBytesRead,    
                   NULL) ;    
   
    if(nBytesRead!=512){   
        printf("Write Error!\n");   
        return FALSE;   
    }   
    else{   
        printf("Write %x\n",wpSector);   
        ;   
    }   
    return TRUE;   
}   
   
void Hard_write_byte(U8 d)   
{   
    if(wp-wbuf==0x200){   
        Hard_write_open(wpSector+1);   
    }   
    *wp=d;   
    wp++;   
    return;   
   
}   
   
s_format Hard_format()   
{   
}      
   
int main()   
{   
    int i;   
    Byte c;   
    Byte fname[12]={"ABCDEFGHSYS"};   
    Byte stuff[]={"Just a try!xxxx"};   
   
    hDevice = CreateFile("\\\\.\\PhysicalDrive1",  // drive to open    
                    MAXIMUM_ALLOWED,                // MAXIMUM_ALLOWED to the drive    
                    FILE_SHARE_READ | // share mode    
                    FILE_SHARE_WRITE,    
                    NULL,             // default security attributes    
                    OPEN_EXISTING,    // disposition    
                    0,                // file attributes    
                    NULL);            // do not copy file attributes    
   
    if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive    
    {   
        return (FALSE);   
    }   
    printf("PhysicalDrive1 opened!\n");   
    rbuf=(BYTE *)malloc(512);   
    memset(rbuf,0xaa,512);   
    wbuf=(BYTE *)malloc(512);   
    memset(wbuf,0xaa,512);   
   
   
    if(fat_install() == OK)   
        printf("\nfat install ok!\n");   
   
    if(fat_get_root_directory (FILE_DIR)==OK)   
        printf("\nroot directory found!\n");   
   
    fat_check_ext();   
   
    if(fat_get_root_directory(FILE_SYS)==OK)   
        printf("OK %s\n",fat_get_name());   
    if(file_seek_next(FILE_SYS,0) ==OK)   
        printf("OK %s\n",fat_get_name());   
   
    if(fat_fopen(READ) != OK)   
        return 0;   
   
    for (i = 0; i < 256; i++){   
        c=fat_fgetc();   
        if(i%16==0)   
            printf("\n");   
        if(c<16)   
            printf("0%x ",c);   
        else   
            printf("%c ",c);   
    }   
    printf("\n***************\n");   
       
    fat_fclose();   
   
   
    if(fat_fcreate(fname, ATTR_FILE) != OK)   
        return 0;   
       
    for(i=0;i<15;i++){   
        fat_fputc(stuff[i]);   
        printf("%c",stuff[i]);   
   
    }   
    fat_fclose();   
/*  
    if(fat_fopen(WRITE) != OK)  
        return 0;  
    for (i = 0; i < 16; i++){  
        fat_fputc(stuff[i]);  
        printf("%c",stuff[i]);  
    }     
    fat_fclose();  
*/   
       
    free(rbuf);   
    free(wbuf);   
    CloseHandle(hDevice);   
    return 0;   
   
}   
#endif //__FLASH    
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
/*=======================================================================*/   
#ifndef __FLASH    
   
FILE *fp;               // handle to the drive to be examined     
BYTE *buf,*p;   
   
bit Hard_read_open(Uint32 sector)   
{   
    DWORD nBytesRead, offset;   
    int i;   
   
    offset=512*sector;   
   
    fseek(fp,offset,SEEK_SET);   
    // Attempt a synchronous read operation.    
    nBytesRead=fread(buf,1,512,fp);   
   
    if(nBytesRead!=512){   
        printf("Read Error!\n");   
        return KO;   
    }   
    else   
        printf("%x",sector);   
/*  
        p=buf;  
    for(i=0; i<512; i++){  
        if(i%16==0)  
            printf("\n");  
        if(*p<16)  
            printf("0%x ",*p);  
        else  
            printf("%x ",*p);  
        p++;  
    }  
*/   
    p=buf;   
    return OK;   
}   
   
void Hard_read_close()   
{   
}   
   
Byte Hard_read_byte()   
{   
    return *p++;   
}   
   
bit Hard_write_open(Uint32 sector)   
{   
}   
   
void Hard_write_close()   
{   
}   
   
void Hard_write_byte(Byte d)   
{   
}   
   
s_format* Hard_format()   
{   
}   
   
Byte HIGH(Uint16 word)   
{   
    Byte i;   
    i=word>>8;   
    return i;   
}   
   
Byte LOW(Uint16 word)   
{   
    Byte i;   
    i=word;   
    return i;   
}   
   
int main()   
{   
    fp=fopen("floppy.bin","rb");   
    if (!fp) // cannot open the drive    
    {   
        return (FALSE);   
    }   
    printf("floppy.bin opened!\n");   
    buf=(BYTE *)malloc(512);   
    memset(buf,0xaa,512);   
   
    if(fat_install() == OK)   
        printf("fat install ok!\n");   
   
    if(fat_get_root_directory (FILE_XXX)==OK)   
        printf("root directory found!\n");   
   
    free(buf);   
    fclose(fp);   
    return 0;   
   
}   
#endif //__FLASH  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -