The stream is positioned at the beginning of the file.(以擦除方式打开文件或创建文件,该文件只能被写入,流指针定位在文件开头)
w+ Open for reading and writing. The file is created if it does
not exist, otherwise it is truncated. The stream is positioned
at the beginning of the file.(以读写方式打开或创建文件,该文件会被擦除,流指针定位在文件开头)
a Open for appending (writing at end of file). The file is cre‐
ated if it does not exist. The stream is positioned at the end
of the file.(以追加的方式在文件末尾写数据,如果文件不存在,则创建,流指针定位在文件末尾)
a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. The initial file position
for reading is at the beginning of the file, but output is
always appended to the end of the file.(以读写追加的方式打开,文件不存在就创建,读指针在文件头,写指针在文件末尾)
The mode string can also include the letter 'b' either as a last char‐
acter or as a character between the characters in any of the two-char‐
acter strings described above. This is strictly for compatibility with
C89 and has no effect; the 'b' is ignored on all POSIX conforming sys‐
tems, including Linux. (Other systems may treat text files and binary
files differently, and adding the 'b' may be a good idea if you do I/O
to a binary file and expect that your program may be ported to non-UNIX
environments.)针对于非unix类操作系统,想要读取二进制文件,可以加字母b
创建文件
读取文件
fread
/\*
\*函数名:fread
\*函数功能:从文件流中读出内容
\*函数参数:
\* void \*ptr:想要存储读出数据的内存首地址
\* size\_t size:想要读取块所占的大小
\* size\_t nmemb:想要读取的块的个数
\* FILE \*stream:被读的文件流指针
\*函数返回值:int:成功返回读成功块的个数,否则返回EOF或者0或者其他值。
\*/
size\_t fread(void \*ptr, size\_t size, size\_t nmemb, FILE *stream);
往文件中写入数据
fwrite
/*
\*函数名:fwrite
\*函数功能:往文件流中写入内容
\*函数参数:
\* void \*ptr:存储想要写的数据的内存首地址
\* size\_t size:想要写的块所占的大小
\* size\_t nmemb:想要写的块的个数
\* FILE \*stream:被写的文件流指针
\*函数返回值:int:成功返回写成功块的个数,否则返回EOF或者0或者其他值。
\*/
size\_t fwrite(const void \*ptr, size\_t size, size\_t nmemb,
FILE *stream);
关闭文件
fclose
/*需要添加的头文件*/
#include <stdio.h>
/*
\*函数名:fclose
\*函数功能:关闭一个文件流
\*函数参数:
\* FILE \*:想要关闭的流指针
\*函数返回值:int:成功返回0,失败返回错误码
\*/
int fclose(FILE *fp);
文件重定位:
fseek
/*
\*函数名:fseek
\*函数功能:
\*函数参数:
\* FILE \*stream:被重定位的文件流指针
\* long offset:是相对于基准的偏移量
\* int whence:基准
\*函数返回值:key\_t:成功返回0,否则返回-1或者错误码。
\*/
int fseek(FILE \*stream, long offset, int whence);
基准值的取值:
SEEK\_SET:文件头
SEEK\_CUR:当前流指针的位置
SEEK\_END:文件末尾
offset > 0:往文件末尾的方向移动
offset < 0:往文件头的方向移动
ftell
/*
*函数名:ftell
*函数功能:
*函数参数:
* FILE *stream:想要获取位置的文件流指针
*函数返回值:long:返回文件流指针的位置
*/
long ftell(FILE *stream);
注意:打开文件和关闭文件配对存在,使用完就得关闭
如何判断是否到文件末尾:
feof()
时间处理:
linux下C获取系统时间的方法:
asctime(将时间和时间以字符串格式表示)
相关函数:
time
ctime
gmtime
localtime
/*需要包含的头文件*/
#include <time.h>
/\*
\*函数名:time
\*函数功能:返回从 1970-01-01 00:00:00 +0000 (UTC).到现在的秒数。
* ```
如果参数t非空,返回值也会存入t中。
*函数参数:
time_t *t:t是一个time_t类型的指针,指向的内存空间用于存储秒数。
\*函数返回值:成功返回从1970年到现在的秒数,失败返回-1/错误码
\*/
time\_t time(time\_t \*t);
/*需要包含的头文件*/
#include <time.h>
/\*
\*函数名:ctime
\*函数功能:将日期和时间转换为Broken-down时间或ASCII
\*函数参数:
* ```
time_t *timep:从time函数获取到的时间
*函数返回值:char *成功返回时间的描述,失败返回NULL
*/
char *ctime(const time_t *timep);
Broken-down time is stored in the structure tm which is defined in
<time.h> as follows:
struct tm {
int tm_sec; /* seconds /
int tm_min; / minutes /
int tm_hour; / hours /
int tm_mday; / day of the month /
int tm_mon; / month /
int tm_year; / year /
int tm_wday; / day of the week /
int tm_yday; / day in the year /
int tm_isdst; / daylight saving time */
};
The members of the tm structure are:
tm_sec The number of seconds after the minute, normally in the range
0 to 59, but can be up to 60 to allow for leap seconds.
tm_min The number of minutes after the hour, in the range 0 to 59.
tm_hour The number of hours past midnight, in the range 0 to 23.
tm_mday The day of the month, in the range 1 to 31.
tm_mon The number of months since January, in the range 0 to 11.
tm_year The number of years since 1900.
tm_wday The number of days since Sunday, in the range 0 to 6.
tm_yday The number of days since January 1, in the range 0 to 365.
tm_isdst A flag that indicates whether daylight saving time is in
effect at the time described. The value is positive if day‐
light saving time is in effect, zero if it is not, and nega‐
tive if the information is not available.
例子:(打印当前时间)
#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
char \*p = NULL;
printf("%s",ctime(&t));
return 0;
}
/需要包含的头文件/
#include <time.h>
/*
*函数名:gmtime
*函数功能:将time_t转换为struct tm类型
*函数参数:
const time_t *timep:time函数获取到的
\*函数返回值:
* ```
struct tm *:成功返回结构体指针,失败返回NULL
*/
struct tm *gmtime(const time_t *timep);
/需要包含的头文件/
#include <time.h>
/*
*函数名:localtime
*函数功能:将time_t转换为struct tm类型
*函数参数:
-
const time_t *timep:time函数获取到的
\*函数返回值:
* ```
struct tm *:成功返回结构体指针,失败返回NULL
*/
struct tm *localtime(const time_t *timep);
/需要包含的头文件/
#include <time.h>
/*
*函数名:asctime
*函数功能:将struct tm转换为char *类型
*函数参数:
-
struct tm *:gmtime获取到的结构体指针
\*函数返回值:
* ```
char *:成功返回字符串形式的时间指针,失败返回NULL
*/
char *asctime(const struct tm *tm);
例子:
#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
printf("%s",asctime(gmtime(&t)));
return 0;
}
建议使用ctime:获取的是当前时区时间/(时区由系统决定)
不建议使用asctime:获取的是格林尼治时间
文件夹操作:
增:mkdir
删:rmdir
查:readdir
打开:opendir/fdopendir
关闭:closedir
1.创建一个文件夹:
/*需要添加的头文件*/
#include <sys/stat.h>
#include <sys/types.h>
/\*函数声明
\*函数名:mkdir
\*函数功能:创建一个文件夹
\*函数参数:
* ```
const char *pathname:被创建的文件夹的名字(带路径)
-
mode_t mode:(被创建的文件夹的权限)(mode & ~umask & 0777).
*函数返回值:成功返回0,失败返回-1或者错误码
*/
int mkdir(const char *pathname, mode_t mode);
umask:文件权限掩码,002(八进制)
775 & mode
例子:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
if(argc != 2)
{
puts(“Usage:./a.out dirname.”);
return -1;
}
mkdir(argv[1], 0777);
return 0;
}
2.删除一个空文件夹:
/需要添加的头文件/
#include <unistd.h>
/*
*函数名:rmdir
*函数功能:删除一个空的文件夹
*函数参数:
-
const char *pathname:想要删除的文件夹的名字(带路径)
\*函数返回值:成功返回0,失败返回-1或者错误码
\*/
int rmdir(const char \*pathname);
例子:
3.打开一个文件夹
/*需要添加的头文件*/
#include <sys/types.h>
#include <dirent.h>
/\*
\*函数名:opendir
\*函数功能:打开一个文件夹
\*函数参数:const char \*name:文件夹名
*函数返回值:DIR*:成功返回一个存储文件夹基本信息的结构体指针(即文件夹流指针),失败返回NULL或错误码
\*/
DIR \*opendir(const char *name);
/*
\*函数名:fdopendir
\*函数功能:打开一个文件夹
## 最后
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数网络安全工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
![img](https://img-blog.csdnimg.cn/img_convert/f383ae185cfef50e1bf19259698ca08a.png)
![img](https://img-blog.csdnimg.cn/img_convert/3414b83ca299221db8726523af6e1834.png)
![img](https://img-blog.csdnimg.cn/img_convert/90866ff9205224c3077dea26541d066c.png)
![img](https://img-blog.csdnimg.cn/img_convert/c1b0348974d237157aea0240299f369f.png)
![img](https://img-blog.csdnimg.cn/img_convert/630779569a251f5bb1964ff1c6585fa8.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点!真正的体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618653875)
**由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**
全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
[外链图片转存中...(img-txd8YPjC-1715798437719)]
[外链图片转存中...(img-e6V1eDLg-1715798437719)]
[外链图片转存中...(img-IVRAcaaU-1715798437720)]
[外链图片转存中...(img-yxTaWVjH-1715798437720)]
[外链图片转存中...(img-1LBih2jN-1715798437720)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点!真正的体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618653875)
**由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**