一个很奇葩的现象出现在我的面前:
昨天用open函数创建文件并保存关闭之后,文件出现了 S 和 T 的权限位,一开始不知道这两个权限位是什么,一查才知道这两个权限位不是什么好东西。

但是,奇怪的是,这个bug不好复现,什么时候出现全看它心情。。。
昨天备课的时候好好的,上课的时候就出bug了,今天想复现,又好好的。

#include<sys/types.h>
#include<sys/stat.h>
#include<iostream>
#include<unistd.h>
#include<fcntl.h>

using namespace std;

int main(){
	char* buf = new char[8];	//can't alloc 1024?
	int fd = open("./fileIO.txt", O_CREAT|O_RDWR|O_APPEND);
	
	//chmod("fileIO.txt",644);
	//const char* filename
	//flag O_CREAT|O_RDONLY|O_WRONLY|O_RDWR|O_APPEND


	if(fd<0){
		cout<<"open file failed!"<<endl;
	}

	int m = write(fd,"test\n",50);
	//int file_id
	//char* buf
	//int lenth
	//return real lenth

	cout<<m<<endl;
	
	close(fd);

	fd = open("./fileIO.txt",O_RDONLY);
	int n = read(fd,buf,500);
	//char* buf (empty buf)	

	cout<<n<<endl;
	printf("%s\n",buf);
	close(fd);
	return 0;
}

经验:以后创建文件的时候,要顺道把权限位设置了,用代码设置,不要手动