2024年嵌入式学习DAY17 --- IO进程的学习_嵌入式 io进程(1)

* const char *path:带路径的文件名
* const char *mode:打开方式
*函数返回值:FILE *:成功返回打开的流指针,失败返回NULL,并且返回错误码
*/
FILE *fopen(const char *path, const char *mode);



	 The argument mode points to a string beginning with one of the  follow‐
       ing sequences (Additional characters may follow these sequences.):

       r      Open  text  file  for  reading.  The stream is positioned at the
              beginning of the file.(以读方式打开文本文件,流指针定位在文件开头)

       r+     Open for reading and writing.  The stream is positioned  at  the
              beginning of the file.(以读写方式打开文件,流指针定位在文件开头)

       w      Truncate  file  to  zero length or create text file for writing.
              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
*函数功能:打开一个文件夹
*函数参数:int fd:文件夹的文件描述符
函数返回值:DIR:成功返回一个存储文件夹基本信息的结构体指针,失败返回NULL或错误码
*/
DIR *fdopendir(int fd);

/* Directory stream type.该格式用于守护进程维护的目录
The Hurd directory format is the same as struct dirent', soreaddir’
returns a pointer into the buffer we read directory data into. */
struct __dirstream
{
void __fd; / struct hurd_fd' pointer for descriptor. */ char *__data; /* Directory block. */ int __entry_data; /* Entry number__data’ corresponds to. */
char __ptr; / Current pointer into the block. /
int __entry_ptr; /
Entry number `__ptr’ corresponds to. /
size_t __allocation; /
Space allocated for the block. /
size_t __size; /
Total valid data in the block. /
__libc_lock_define (, __lock) /
Mutex lock for this structure. */
};

/* Directory stream type.

The miscellaneous Unix readdir' implementations read directory data into a buffer and returnstruct dirent *’ pointers into it. */

struct __dirstream
{
int fd; /* File descriptor. /
__libc_lock_define (, lock) /
Mutex lock for this structure. /
size_t allocation; /
Space allocated for the block. /
size_t size; /
Total valid data in the block. /
size_t offset; /
Current offset into the block. /
off_t filepos; /
Position of next entry to read. /
int errcode; /
Delayed error code. /
/
Directory block. We must make sure that this block starts
at an address that is aligned adequately enough to store
dirent entries. Using the alignment of “void *” is not
sufficient because dirents on 32-bit platforms can require
64-bit alignment. We use “long double” here to be consistent
with what malloc uses. */
char data[0] attribute ((aligned (alignof (long double))));
};

4.关闭文件夹
/需要添加的头文件/
#include <sys/types.h>
#include <dirent.h>
/*函数声明
*函数名:closedir
*函数功能:关闭一个文件夹
*函数返参数:DIR *dirp:想要关闭的文件夹的流指针
*函数返回值:int:成功返回0,失败返回-1或者错误码
*/
int closedir(DIR *dirp);

5.读取文件夹的内容
/需要添加的头文件/
#include <dirent.h>
/*函数声明
*函数名:readdir
*函数功能:读取文件夹的信息
*函数参数:DIR *dirp:打开后的文件夹的流指针
*函数返回值:struct dirent *:成功返回文件夹信息的指针,失败返回NULL或错误码
*/
struct dirent *readdir(DIR *dirp);

struct dirent
{
#ifdef __USE_FILE_OFFSET64
__ino64_t d_ino;
#else
__ino_t d_ino;
int __pad;
#endif
__off_t d_off;
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256]; /* We must not include limits.h! */
};

struct dirent
{
ino_t d_ino; /* inode number /
off_t d_off; /
offset to the next dirent /
unsigned short d_reclen; /
length of this record /
unsigned char d_type; /
type of file; not supported by all file system types /
char d_name[256]; /
filename */
};

d_type:

DT_BLK This is a block device.
DT_CHR This is a character device.
DT_DIR This is a directory.
DT_FIFO This is a named pipe (FIFO).
DT_LNK This is a symbolic link.
DT_REG This is a regular file.
DT_SOCK This is a UNIX domain socket.
DT_UNKNOWN The file type is unknown.

例子:

写在最后

在结束之际,我想重申的是,学习并非如攀登险峻高峰,而是如滴水穿石般的持久累积。尤其当我们步入工作岗位之后,持之以恒的学习变得愈发不易,如同在茫茫大海中独自划舟,稍有松懈便可能被巨浪吞噬。然而,对于我们程序员而言,学习是生存之本,是我们在激烈市场竞争中立于不败之地的关键。一旦停止学习,我们便如同逆水行舟,不进则退,终将被时代的洪流所淘汰。因此,不断汲取新知识,不仅是对自己的提升,更是对自己的一份珍贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。

需要完整版PDF学习资源私我

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以点击这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 30
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值