1 / 24

Project2: 文件同步工具 (2)

Project2: 文件同步工具 (2). 其他问题. 移植到 windows 平台 linux 下的 API 换成 windows 平台的相应 API. 提高要求:移植到 windows 平台. 基本思路 Linux 下的函数换 成 windows 平台的相应函数 方法 1 : DOS 命令 方法 2 : API 调用. 方法 1 :. int system(char *command); c 的标准库中, #include < stdio.h > #include < stdlib.h > 发出一个 DOS 命令

Download Presentation

Project2: 文件同步工具 (2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Project2: 文件同步工具(2)

  2. 其他问题 • 移植到windows平台 • linux下的API换成windows平台的相应API

  3. 提高要求:移植到windows平台 • 基本思路 • Linux下的函数换成windows平台的相应函数 • 方法1:DOS命令 • 方法2:API调用

  4. 方法1: • intsystem(char *command); • c的标准库中,#include<stdio.h>#include <stdlib.h> • 发出一个DOS命令 • int stat(const char * file_name, struct stat *buf); • 与linux下lstat函数使用类似

  5. 使用举例 • #include <stdlib.h> • #include <stdio.h> • #include <sys/stat.h> • int main() • { • struct stat status; • printf("About to spawn command.com and run a DOS command\n"); • system("dir"); • printf("About to get file size\n"); • stat(”.\\test.cpp”, &status); • printf("%d\n”, status.st_size); • return 0; • }

  6. 方法2: • WindowsAPI函数 • FindFirstFile • FindNextFile • FindClose • GetFileTime

  7. Findfirstfile • 查找指定目录的第一个文件或目录并返回它的句柄 • 函数原型: • HANDLE FindFirstFile( • LPCTSTR lpFileName, // 目录名 • LPWIN32_FIND_DATAlpFindFileData // 数据缓冲区 • ); • 参数: • lpFileName:指向字符串的指针用于指定一个有效的目录。 • lpFindFileData:指向一个WIN32_FIND_DATA的指针,用于存放找到文件或目录的信息。 • 返回值 • 如果成功,则返回找到文件或目录的句柄。在FindNextFile和FindClose函数中会用到此句柄。 • 如果失败,返回INVALID_HANDLE_VALUE。要获得更多的信息调用GetLastError函数。 • 注:指定目录的形式应该为"..\\abc\\*.*" 就是在abc目录中找第一个文件或目录。

  8. WIN32_FIND_DATA的结构中包含很多文件或目录的有用信息,后面的程序或许会用到。该结构如下:WIN32_FIND_DATA的结构中包含很多文件或目录的有用信息,后面的程序或许会用到。该结构如下: • typedefstruct _WIN32_FIND_DATA { • DWORD dwFileAttributes; • FILETIME ftCreationTime; • FILETIME ftLastAccessTime; • FILETIME ftLastWriteTime; • DWORD nFileSizeHigh; • DWORD nFileSizeLow; • DWORD dwReserved0; • DWORD dwReserved1; • TCHAR cFileName[ MAX_PATH ]; • TCHAR cAlternateFileName[ 14 ]; • } WIN32_FIND_DATA, *PWIN32_FIND_DATA;

  9. FindNextFile • BOOL FindNextFile( • HANDLE hFindFile, //searchhandle • LPWIN32_FIND_DATA lpFindFileData //databuffer • ); • 参数说明 • HANDLEhFindFile • 搜索的文件句柄 函数执行的时候搜索的是此句柄的下一文件 • LPWIN32_FIND_DATA lpFindFileData • 指向一个用于保存文件信息的结构体 • 返回值 • 非零表示成功,零表示失败。

  10. FindClose BOOL FindClose( HANDLE hFindFile // file search handle ); • 功能:关闭FindFirstFile创建的搜索句柄 • 参数: • HANDLE hFindFileFindFirstFile创建的句柄

  11. 程序举例 #include<stdio.h> #include <windows.h> int main( ) { WIN32_FIND_DATA FileData; HANDLE hSearch = NULL; BOOL unfinished= TURE; hSearch = FindFirstFile("*.*", &FileData); // find for all files if(hSearch == INVALID_HANDLE_VALUE) return; // No file found while(unfinished) { puts(FileData.cFileName); // print file name unfinished = FindNextFile(hSearch, &FileData); // find next file } FindClose(hSearch); // finish searching return 0; }

  12. GetFileTime BOOL GetFileTime( HANDLE hFile, // identifies the file LPFILETIME lpCreationTime, // address of creation time LPFILETIME lpLastAccessTime, // address of last access time LPFILETIME lpLastWriteTime // address of last write time ); • 返回的文件时间采用UTC格式 • 可以使用FileTimeToSystemTime方法,将文件时间转换为系统时间格式。

  13. 编程实践:图书管理程序 • 最简单的图书管理系统 • 提供图书查询、插入、编辑、删除的功能 • 要求: • 分为3个模块(每个模块一个单独的c/cpp文件) • 界面显示 • 数据的加载和存储模块 • 查询 • 目的 • 练习linux下编写C/C++普通程序 • 练习编写多文件的程序 • 能够简单的使用makefile

  14. 示例

  15. 多文件C程序 • 模块化设计 • 大程序自上向下进行功能分解,分成若干个子模块,模块有自己的界面,有相关的操作,完成独立的功能。各模块可以分别由不同的人员编写,最后,将不同的模块组装成一个完整的程序。在C语言中,用函数实现功能模块的定义,一个完整的C程序可以由多个源程序文件组成,一个文件中可以包含多个函数 • 节约编译时间 • 修改了个别源代码文件后,只需重新编译修改过的文件及受它影响的文件,并再次连接即可,而不需要重新编译不受影响的文件。这样就可以节约不少编译时间。

  16. 一个最简单的多文件程序示例

  17. 例程说明 • 程序包含三个文件 • main.c • add.h • add.c • 头文件 –add.h • 头文件中一般定义了函数的声明、结构体的定义、宏定义。(常量和全局变量最好放到源文件中) • 头文件中放函数的声明,函数由源文件实现:接口和实现分开 • 头文件可通过宏定义来保证类定义、结构体定义、宏定义的唯一性。确实很方便,不容易出错。

  18. add.h • #ifndef … #define … #endif • 条件编译指令 • 防止重复包含相同的内容

  19. 课后作业 • 编写图书管理程序 • 多文件 • 要有头文件 • 写makefile

  20. 期末安排 • 6月5日: • 上课时间,HTML5 大作业答辩 • 地点:机房 • 6月12日: • 上机时间,随堂测验 • 上机编程 • 1 ~ 2 题 • Project2或者图书管理系统的子任务,YOJ上提交 • 占总评的30%

  21. 简易Makefile • 示例 • 3个头文件 • 8个c文件

  22. 简易Makefile (2)

  23. 简易Makefile(3)

More Related