本人有dos系统,想用它的ctrl+break中断,在它的中断服务程序中进行文件操作,就是读一文件,写到另一个文件中。可是在操作文件时,老是司机,不知道为何原因。请各位大人不吝执教!QQ:36014789
程序如下
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include <sys b.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <share.h>
#include <IO.H>
#ifdef __cplusplus
#define __ARGU ...
#else
#define __ARGU
#endif
void interrupt (*oldhandler)(void); /*函数声明*/
void interrupt newint(__ARGU); /*函数声明*/
void install (void interrupt (*fadd)(__ARGU), int num);
main()
{
install (newint,0x1b); /*Ctrl+Break中断号:1BH*/
keep(0,_SS+(_SP/16)-_psp); /*驻留程序*/
return 0;
}
void interrupt newint(__ARGU)
{ int fd1=-1; //读文件号,由open函数返回;
int fd2=-1; //写文件号,由open函数返回;
int read_res=-1; //read函数返回值,-1表示出错;
int write_res=-1; //write函数返回值,-1表示出错;
char buf[10]; //缓冲区; //读文件号,由open函数返回;
disable();
fd1=open("c:\\fr.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
fd2=open("c:\\fw.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
if(fd1==-1||fd2==-1)
puts("Open file failure!\n");
else
{
puts("Open files success!\n");
//不能用fseek函数,牵涉到dos重入问题,只能用非缓冲文件系统函数;
lseek(fd1,0,SEEK_SET);//移动文件位置指针到指定位置;
//不能用fread函数,牵涉到dos重入问题,只能用非缓冲文件系统函数;
read_res=read(fd1,buf,COUNT); //读文件函数;
if(read_res==-1) //如果读文件失败;
puts("Read file failure!\n");
else
{
puts("Read files success!\n");
//不能用fseek函数,牵涉到dos重入问题,只能用非缓冲文件系统函数;
lseek(fd2,0,SEEK_SET);//移动文件位置指针到指定位置;
//不能用fwrite函数,牵涉到dos重入问题,只能用非缓冲文件系统函数;
write_res=write(fd2,buf,COUNT); //写文件函数;
if(write_res==-1) //如果写文件失败;
puts("Write file failure!\n");
else
puts("Write files success!\n");
}
close(fd1); //关闭文件;
close(fd2); //关闭文件;
}
enable();
}
void install (void interrupt (*fadd)(__ARGU), int num)
{
disable(); //禁止中断;
setvect(num,fadd); /*设置中断*/
enable(); //开中断;
}
程序如下
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include <sys b.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <share.h>
#include <IO.H>
#ifdef __cplusplus
#define __ARGU ...
#else
#define __ARGU
#endif
void interrupt (*oldhandler)(void); /*函数声明*/
void interrupt newint(__ARGU); /*函数声明*/
void install (void interrupt (*fadd)(__ARGU), int num);
main()
{
install (newint,0x1b); /*Ctrl+Break中断号:1BH*/
keep(0,_SS+(_SP/16)-_psp); /*驻留程序*/
return 0;
}
void interrupt newint(__ARGU)
{ int fd1=-1; //读文件号,由open函数返回;
int fd2=-1; //写文件号,由open函数返回;
int read_res=-1; //read函数返回值,-1表示出错;
int write_res=-1; //write函数返回值,-1表示出错;
char buf[10]; //缓冲区; //读文件号,由open函数返回;
disable();
fd1=open("c:\\fr.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
fd2=open("c:\\fw.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
if(fd1==-1||fd2==-1)
puts("Open file failure!\n");
else
{
puts("Open files success!\n");
//不能用fseek函数,牵涉到dos重入问题,只能用非缓冲文件系统函数;
lseek(fd1,0,SEEK_SET);//移动文件位置指针到指定位置;
//不能用fread函数,牵涉到dos重入问题,只能用非缓冲文件系统函数;
read_res=read(fd1,buf,COUNT); //读文件函数;
if(read_res==-1) //如果读文件失败;
puts("Read file failure!\n");
else
{
puts("Read files success!\n");
//不能用fseek函数,牵涉到dos重入问题,只能用非缓冲文件系统函数;
lseek(fd2,0,SEEK_SET);//移动文件位置指针到指定位置;
//不能用fwrite函数,牵涉到dos重入问题,只能用非缓冲文件系统函数;
write_res=write(fd2,buf,COUNT); //写文件函数;
if(write_res==-1) //如果写文件失败;
puts("Write file failure!\n");
else
puts("Write files success!\n");
}
close(fd1); //关闭文件;
close(fd2); //关闭文件;
}
enable();
}
void install (void interrupt (*fadd)(__ARGU), int num)
{
disable(); //禁止中断;
setvect(num,fadd); /*设置中断*/
enable(); //开中断;
}
