22.11.8 IO Day2 读写,权限

1、读写,权限

ubuntu@ubuntu:~/yuyu/yu/1/4$ cat 8.c
r   以读的方式打开文件.
	如果文件不存在,则函数运行失败,文件打开失败;
	如果文件存在,则函数运行成功,文件打开成功;
	//O_RDONLY
r+ 	以读写的方式打开文件.
	如果文件不存在,则函数运行失败,文件打开失败;
	如果文件存在,则函数运行成功,文件打开成功;
	//O_RDWR
w   以写的方式打开文件.
	如果文件不存在,则创建文件,并打开文件;
	如果文件存在,则清空文件,并打开文件; 
	// O_WRONLY|O_CREAT|O_TRUNC
w+  以读写的方式打开文件.
	如果文件不存在,则创建文件,并打开文件;
	如果文件存在,则清空文件,并打开文件;
	//O_RDWR|O_CREAT|O_TRUNC
a 	以追加写的方式打开文件;
	如果文件不存在,则创建文件,并打开文件;
	如果文件存在,则从文件结尾位置开始写入; 
	//O_APPEND|O_CREAT|O_TRUNC
a+ 	以读和追加写的方式打开文件;
	如果文件不存在,则创建文件,并打开文件;
	如果文件存在,
	读:从文件开头读;
	写:从文件结尾位置写入;
	//O_RDONLY|O_APPEND|O_CREAT|O_TRUNC
	
	mode_t mode:权限,文件创建时候的权限;可以填八进制权限:0777 0664;
如果flags中没有包含O_CREAT 或者O_TMPFILE,则会忽略mode参数;


ubuntu@ubuntu:~/yuyu/yu/1/4$ 

2、在文件内打印时间及其行号

ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
9.c: In function ‘main’:
9.c:18:3: error: ‘t’ undeclared (first use in this function)
   t = time(NULL);
   ^
9.c:18:3: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
9.c: In function ‘main’:
9.c:18:9: error: expected identifier or ‘(’ before ‘=’ token
  size_t = time(NULL);
         ^
9.c:19:21: error: ‘t’ undeclared (first use in this function)
   info = localtime(&t);
                     ^
9.c:19:21: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time.txt
[1] 2022-11-08 17:07:24
[2] 2022-11-08 17:07:25
[3] 2022-11-08 17:07:26
[4] 2022-11-08 17:07:27
[5] 2022-11-08 17:07:28
[6] 2022-11-08 17:07:29
[7] 2022-11-08 17:07:30
[8] 2022-11-08 17:07:31
[9] 2022-11-08 17:07:32
[10] 2022-11-08 17:07:33
[11] 2022-11-08 17:07:34
[12] 2022-11-08 17:07:35
[13] 2022-11-08 17:07:36
[14] 2022-11-08 17:07:37
[15] 2022-11-08 17:07:38
[16] 2022-11-08 17:07:39
[17] 2022-11-08 17:07:40
[18] 2022-11-08 17:07:41
[19] 2022-11-08 17:07:42
[20] 2022-11-08 17:07:43
[21] 2022-11-08 17:07:44
[22] 2022-11-08 17:07:45
[23] 2022-11-08 17:07:46
[24] 2022-11-08 17:07:47
[25] 2022-11-08 17:07:48
[26] 2022-11-08 17:07:49
[27] 2022-11-08 17:07:50
[28] 2022-11-08 17:07:51
[29] 2022-11-08 17:07:52
[30] 2022-11-08 17:07:53
[31] 2022-11-08 17:07:54
[32] 2022-11-08 17:07:55
[33] 2022-11-08 17:07:56
[34] 2022-11-08 17:07:57
[35] 2022-11-08 17:07:58
[36] 2022-11-08 17:07:59
[37] 2022-11-08 17:08:00
[38] 2022-11-08 17:08:01
[39] 2022-11-08 17:08:02
[40] 2022-11-08 17:08:03
[41] 2022-11-08 17:08:04
[42] 2022-11-08 17:08:05
[43] 2022-11-08 17:08:06
[44] 2022-11-08 17:08:07
[45] 2022-11-08 17:08:08
[46] 2022-11-08 17:08:09
[47] 2022-11-08 17:08:10
[48] 2022-11-08 17:08:11
[49] 2022-11-08 17:08:12
[50] 2022-11-08 17:08:13
[51] 2022-11-08 17:08:14
[52] 2022-11-08 17:08:15
[53] 2022-11-08 17:08:16
[54] 2022-11-08 17:08:17
[55] 2022-11-08 17:08:18
[56] 2022-11-08 17:08:19
[57] 2022-11-08 17:08:20
[58] 2022-11-08 17:08:21
[59] 2022-11-08 17:08:22
[60] 2022-11-08 17:08:23
[61] 2022-11-08 17:08:24
[62] 2022-11-08 17:08:25
[63] 2022-11-08 17:08:26
[64] 2022-11-08 17:08:27
[65] 2022-11-08 17:08:28
[66] 2022-11-08 17:08:29
[67] 2022-11-08 17:08:30
[68] 2022-11-08 17:08:31
[69] 2022-11-08 17:08:32
[70] 2022-11-08 17:08:33
[71] 2022-11-08 17:18:41
[72] 2022-11-08 17:18:42
[73] 2022-11-08 17:18:43
[74] 2022-11-08 17:18:44
[75] 2022-11-08 17:18:45
[76] 2022-11-08 17:18:46
[77] 2022-11-08 17:25:55
[78] 2022-11-08 17:25:56
[79] 2022-11-08 17:25:57
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time.txt
[1] 2022-11-08 17:07:24
[2] 2022-11-08 17:07:25
[3] 2022-11-08 17:07:26
[4] 2022-11-08 17:07:27
[5] 2022-11-08 17:07:28
[6] 2022-11-08 17:07:29
[7] 2022-11-08 17:07:30
[8] 2022-11-08 17:07:31
[9] 2022-11-08 17:07:32
[10] 2022-11-08 17:07:33
[11] 2022-11-08 17:07:34
[12] 2022-11-08 17:07:35
[13] 2022-11-08 17:07:36
[14] 2022-11-08 17:07:37
[15] 2022-11-08 17:07:38
[16] 2022-11-08 17:07:39
[17] 2022-11-08 17:07:40
[18] 2022-11-08 17:07:41
[19] 2022-11-08 17:07:42
[20] 2022-11-08 17:07:43
[21] 2022-11-08 17:07:44
[22] 2022-11-08 17:07:45
[23] 2022-11-08 17:07:46
[24] 2022-11-08 17:07:47
[25] 2022-11-08 17:07:48
[26] 2022-11-08 17:07:49
[27] 2022-11-08 17:07:50
[28] 2022-11-08 17:07:51
[29] 2022-11-08 17:07:52
[30] 2022-11-08 17:07:53
[31] 2022-11-08 17:07:54
[32] 2022-11-08 17:07:55
[33] 2022-11-08 17:07:56
[34] 2022-11-08 17:07:57
[35] 2022-11-08 17:07:58
[36] 2022-11-08 17:07:59
[37] 2022-11-08 17:08:00
[38] 2022-11-08 17:08:01
[39] 2022-11-08 17:08:02
[40] 2022-11-08 17:08:03
[41] 2022-11-08 17:08:04
[42] 2022-11-08 17:08:05
[43] 2022-11-08 17:08:06
[44] 2022-11-08 17:08:07
[45] 2022-11-08 17:08:08
[46] 2022-11-08 17:08:09
[47] 2022-11-08 17:08:10
[48] 2022-11-08 17:08:11
[49] 2022-11-08 17:08:12
[50] 2022-11-08 17:08:13
[51] 2022-11-08 17:08:14
[52] 2022-11-08 17:08:15
[53] 2022-11-08 17:08:16
[54] 2022-11-08 17:08:17
[55] 2022-11-08 17:08:18
[56] 2022-11-08 17:08:19
[57] 2022-11-08 17:08:20
[58] 2022-11-08 17:08:21
[59] 2022-11-08 17:08:22
[60] 2022-11-08 17:08:23
[61] 2022-11-08 17:08:24
[62] 2022-11-08 17:08:25
[63] 2022-11-08 17:08:26
[64] 2022-11-08 17:08:27
[65] 2022-11-08 17:08:28
[66] 2022-11-08 17:08:29
[67] 2022-11-08 17:08:30
[68] 2022-11-08 17:08:31
[69] 2022-11-08 17:08:32
[70] 2022-11-08 17:08:33
[71] 2022-11-08 17:18:41
[72] 2022-11-08 17:18:42
[73] 2022-11-08 17:18:43
[74] 2022-11-08 17:18:44
[75] 2022-11-08 17:18:45
[76] 2022-11-08 17:18:46
[77] 2022-11-08 17:25:55
[78] 2022-11-08 17:25:56
[79] 2022-11-08 17:25:57
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time.txt
[1] 2022-11-08 17:07:24
[2] 2022-11-08 17:07:25
[3] 2022-11-08 17:07:26
[4] 2022-11-08 17:07:27
[5] 2022-11-08 17:07:28
[6] 2022-11-08 17:07:29
[7] 2022-11-08 17:07:30
[8] 2022-11-08 17:07:31
[9] 2022-11-08 17:07:32
[10] 2022-11-08 17:07:33
[11] 2022-11-08 17:07:34
[12] 2022-11-08 17:07:35
[13] 2022-11-08 17:07:36
[14] 2022-11-08 17:07:37
[15] 2022-11-08 17:07:38
[16] 2022-11-08 17:07:39
[17] 2022-11-08 17:07:40
[18] 2022-11-08 17:07:41
[19] 2022-11-08 17:07:42
[20] 2022-11-08 17:07:43
[21] 2022-11-08 17:07:44
[22] 2022-11-08 17:07:45
[23] 2022-11-08 17:07:46
[24] 2022-11-08 17:07:47
[25] 2022-11-08 17:07:48
[26] 2022-11-08 17:07:49
[27] 2022-11-08 17:07:50
[28] 2022-11-08 17:07:51
[29] 2022-11-08 17:07:52
[30] 2022-11-08 17:07:53
[31] 2022-11-08 17:07:54
[32] 2022-11-08 17:07:55
[33] 2022-11-08 17:07:56
[34] 2022-11-08 17:07:57
[35] 2022-11-08 17:07:58
[36] 2022-11-08 17:07:59
[37] 2022-11-08 17:08:00
[38] 2022-11-08 17:08:01
[39] 2022-11-08 17:08:02
[40] 2022-11-08 17:08:03
[41] 2022-11-08 17:08:04
[42] 2022-11-08 17:08:05
[43] 2022-11-08 17:08:06
[44] 2022-11-08 17:08:07
[45] 2022-11-08 17:08:08
[46] 2022-11-08 17:08:09
[47] 2022-11-08 17:08:10
[48] 2022-11-08 17:08:11
[49] 2022-11-08 17:08:12
[50] 2022-11-08 17:08:13
[51] 2022-11-08 17:08:14
[52] 2022-11-08 17:08:15
[53] 2022-11-08 17:08:16
[54] 2022-11-08 17:08:17
[55] 2022-11-08 17:08:18
[56] 2022-11-08 17:08:19
[57] 2022-11-08 17:08:20
[58] 2022-11-08 17:08:21
[59] 2022-11-08 17:08:22
[60] 2022-11-08 17:08:23
[61] 2022-11-08 17:08:24
[62] 2022-11-08 17:08:25
[63] 2022-11-08 17:08:26
[64] 2022-11-08 17:08:27
[65] 2022-11-08 17:08:28
[66] 2022-11-08 17:08:29
[67] 2022-11-08 17:08:30
[68] 2022-11-08 17:08:31
[69] 2022-11-08 17:08:32
[70] 2022-11-08 17:08:33
[71] 2022-11-08 17:18:41
[72] 2022-11-08 17:18:42
[73] 2022-11-08 17:18:43
[74] 2022-11-08 17:18:44
[75] 2022-11-08 17:18:45
[76] 2022-11-08 17:18:46
[77] 2022-11-08 17:25:55
[78] 2022-11-08 17:25:56
[79] 2022-11-08 17:25:57
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time.txt
[1] 2022-11-08 17:07:24
[2] 2022-11-08 17:07:25
[3] 2022-11-08 17:07:26
[4] 2022-11-08 17:07:27
[5] 2022-11-08 17:07:28
[6] 2022-11-08 17:07:29
[7] 2022-11-08 17:07:30
[8] 2022-11-08 17:07:31
[9] 2022-11-08 17:07:32
[10] 2022-11-08 17:07:33
[11] 2022-11-08 17:07:34
[12] 2022-11-08 17:07:35
[13] 2022-11-08 17:07:36
[14] 2022-11-08 17:07:37
[15] 2022-11-08 17:07:38
[16] 2022-11-08 17:07:39
[17] 2022-11-08 17:07:40
[18] 2022-11-08 17:07:41
[19] 2022-11-08 17:07:42
[20] 2022-11-08 17:07:43
[21] 2022-11-08 17:07:44
[22] 2022-11-08 17:07:45
[23] 2022-11-08 17:07:46
[24] 2022-11-08 17:07:47
[25] 2022-11-08 17:07:48
[26] 2022-11-08 17:07:49
[27] 2022-11-08 17:07:50
[28] 2022-11-08 17:07:51
[29] 2022-11-08 17:07:52
[30] 2022-11-08 17:07:53
[31] 2022-11-08 17:07:54
[32] 2022-11-08 17:07:55
[33] 2022-11-08 17:07:56
[34] 2022-11-08 17:07:57
[35] 2022-11-08 17:07:58
[36] 2022-11-08 17:07:59
[37] 2022-11-08 17:08:00
[38] 2022-11-08 17:08:01
[39] 2022-11-08 17:08:02
[40] 2022-11-08 17:08:03
[41] 2022-11-08 17:08:04
[42] 2022-11-08 17:08:05
[43] 2022-11-08 17:08:06
[44] 2022-11-08 17:08:07
[45] 2022-11-08 17:08:08
[46] 2022-11-08 17:08:09
[47] 2022-11-08 17:08:10
[48] 2022-11-08 17:08:11
[49] 2022-11-08 17:08:12
[50] 2022-11-08 17:08:13
[51] 2022-11-08 17:08:14
[52] 2022-11-08 17:08:15
[53] 2022-11-08 17:08:16
[54] 2022-11-08 17:08:17
[55] 2022-11-08 17:08:18
[56] 2022-11-08 17:08:19
[57] 2022-11-08 17:08:20
[58] 2022-11-08 17:08:21
[59] 2022-11-08 17:08:22
[60] 2022-11-08 17:08:23
[61] 2022-11-08 17:08:24
[62] 2022-11-08 17:08:25
[63] 2022-11-08 17:08:26
[64] 2022-11-08 17:08:27
[65] 2022-11-08 17:08:28
[66] 2022-11-08 17:08:29
[67] 2022-11-08 17:08:30
[68] 2022-11-08 17:08:31
[69] 2022-11-08 17:08:32
[70] 2022-11-08 17:08:33
[71] 2022-11-08 17:18:41
[72] 2022-11-08 17:18:42
[73] 2022-11-08 17:18:43
[74] 2022-11-08 17:18:44
[75] 2022-11-08 17:18:45
[76] 2022-11-08 17:18:46
[77] 2022-11-08 17:25:55
[78] 2022-11-08 17:25:56
[79] 2022-11-08 17:25:57
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ vi time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ vi time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ vi time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ vi time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ vi time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
[1] 2022-11-08 19:49:03
[2] 2022-11-08 19:49:04
[3] 2022-11-08 19:49:05
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
[1] 2022-11-08 19:49:03
[2] 2022-11-08 19:49:04
[3] 2022-11-08 19:49:05
[1] 2022-11-08 19:49:20
[2] 2022-11-08 19:49:21
[3] 2022-11-08 19:49:22
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
[1] 2022-11-08 19:49:03
[2] 2022-11-08 19:49:04
[3] 2022-11-08 19:49:05
[1] 2022-11-08 19:49:20
[2] 2022-11-08 19:49:21
[3] 2022-11-08 19:49:22
[7] 2022-11-08 19:52:01
[8] 2022-11-08 19:52:02
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
[1] 2022-11-08 19:49:03
[2] 2022-11-08 19:49:04
[3] 2022-11-08 19:49:05
[1] 2022-11-08 19:49:20
[2] 2022-11-08 19:49:21
[3] 2022-11-08 19:49:22
[7] 2022-11-08 19:52:01
[8] 2022-11-08 19:52:02
[9] 2022-11-08 19:52:18
[10] 2022-11-08 19:52:19
[11] 2022-11-08 19:52:20
ubuntu@ubuntu:~/yuyu/yu/1/4$ 

代码:

ubuntu@ubuntu:~/yuyu/yu/1/4$ cat 9.c
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
	FILE *fp = fopen("time1.txt","a+");
	int count=1,c;
	while((c=fgetc(fp))!=-1){
		if(c==10){
			count++;
		}
	}
	struct tm *info = NULL;
	while(1)
	{
//system("clear");

    	size_t t = time(NULL);
		info = localtime(&t);
		fprintf(fp,"[%d] %d-%02d-%02d %02d:%02d:%02d\n",\
				count,info->tm_year+1900,info->tm_mon+1,info->tm_mday,\
				info->tm_hour,info->tm_min,info->tm_sec);
		count++;
		fflush(fp);
		sleep(1);
	}
	fclose(fp);
	return 0;
	
}
ubuntu@ubuntu:~/yuyu/yu/1/4$ 

 

ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
[1] 2022-11-08 19:49:03
[2] 2022-11-08 19:49:04
[3] 2022-11-08 19:49:05
[1] 2022-11-08 19:49:20
[2] 2022-11-08 19:49:21
[3] 2022-11-08 19:49:22
[7] 2022-11-08 19:52:01
[8] 2022-11-08 19:52:02
[9] 2022-11-08 19:52:18
[10] 2022-11-08 19:52:19
[11] 2022-11-08 19:52:20
[12] 2022-11-08 19:55:45
[13] 2022-11-08 19:55:46
ubuntu@ubuntu:~/yuyu/yu/1/4$

 结果:

 

ubuntu@ubuntu:~/yuyu/yu/1/4$ vi time1.txt
ubuntu@ubuntu:~/yuyu/yu/1/4$ gcc 9.c
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ ./a.out
^C
ubuntu@ubuntu:~/yuyu/yu/1/4$ cat time1.txt
[1] 2022-11-08 19:57:20
[2] 2022-11-08 19:57:21
[3] 2022-11-08 19:57:27
[4] 2022-11-08 19:57:28
[5] 2022-11-08 19:57:35
[6] 2022-11-08 19:57:36
ubuntu@ubuntu:~/yuyu/yu/1/4$

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值