首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么用C调用windowsAPI将一个文件的前512个字节写到一个.img中

2012-05-11 
如何用C调用windowsAPI将一个文件的前512个字节写到一个.img中rt,这是我们的作业,要用四种方法,先问问一个

如何用C调用windowsAPI将一个文件的前512个字节写到一个.img中
rt,这是我们的作业,要用四种方法,先问问一个,剩下的三个应该会比较类似
求大神帮帮忙……

[解决办法]
4个方法

1.

C/C++ code
HANDLE WINAPI CreateFile(  __in      LPCTSTR lpFileName,  __in      DWORD dwDesiredAccess,  __in      DWORD dwShareMode,  __in_opt  LPSECURITY_ATTRIBUTES lpSecurityAttributes,  __in      DWORD dwCreationDisposition,  __in      DWORD dwFlagsAndAttributes,  __in_opt  HANDLE hTemplateFile);
[解决办法]
探讨

4个方法

1.C/C++ code
HANDLE WINAPI CreateFile(
__in LPCTSTR lpFileName,
__in DWORD dwDesiredAccess,
__in DWORD dwShareMode,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttribut……

[解决办法]
呵呵,假设有一个文件叫做c_ghost,我们要将它写到floppy.img里。
看看Linux的做法:
Shell的写法:
1. head -c 512 c_ghost > floppy.img
2. dd if=c_ghost of=floppy2.img bs=512 count=1
Linux API的写法:
C/C++ code
#include <stdio.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>int main(void){        int fdin, fdout;        int n;        char buf[512] = {0};        fdin = open("c_ghost", O_RDONLY);        if(fdin < 0) {                perror("");                exit(1);        }        fdout = open("floppy.img", O_WRONLY | O_CREAT);        if(fdout < 0) {                perror("");                exit(1);        }        if((n = read(fdin, buf, 512)) < 512) {                printf("c_ghost is %d bytes, smaller than 512 bytes.\n", n);                exit(1);        }        write(fdout, buf, 512);        close(fdout);        close(fdin);        return 0;} 

热点排行