当前位置:网站首页>Remember the bug encountered in reading and writing a file

Remember the bug encountered in reading and writing a file

2021-08-08 00:55:01 Look, the future

Code 1 :

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

using namespace std;

int main(){
    
	char* buf = new char[8];	//can't alloc 1024?
	int fd = open("./fileIO.txt", O_CREAT|O_RDWR);
	
	if(fd<0){
    
		cout<<"open( file failed!)"<<endl;
	}

	return 0;
}

result , The created file has dangerous permissions ,S and T、


Code 2:

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

using namespace std;

int main(){
    
	char* buf = new char[8];	//can't alloc 1024?
	int fd = open("./fileIO.txt", O_CREAT|O_RDWR);
	
	if(fd<0){
    
		cout<<"open( file failed!)"<<endl;
	}

	int m = write(fd,"test\n",5);
	cout<<m<<endl;

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

result :m Valuable ,n by 0,buf It's definitely empty .


later , I don't think it will be m yes size_t Why , Only a dead horse can be a living horse doctor .

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

using namespace std;

int main(){
    
	char* buf = new char[8];	//can't alloc 1024?
	int fd = open("./fileIO.txt", O_CREAT|O_RDWR);
	
	if(fd<0){
    
		cout<<"open( file failed!)"<<endl;
	}

	int m = write(fd,"test\n",5);
	cout<<m<<endl;
	
	int n = read(fd,buf,5);
	cout<<n<<endl;
	printf("%s\n",buf);
	close(fd);
	return 0;
}

Hey ,m/n It's all worth it , But still buf Nothing 、
sizeof(buf) There are also lengths , But there's nothing .


Finally realized that , When opening the file to write , It's a heavy head cover , The contents of the file have been overwritten , But the new content hasn't been saved yet , The file operation handle is in write, So you can't read the content .

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

using namespace std;

int main(){
    
	char* buf = new char[8];	//can't alloc 1024?
	int fd = open("./fileIO.txt", O_CREAT|O_RDWR);
	
	if(fd<0){
    
		cout<<"open( file failed!)"<<endl;
	}

	int m = write(fd,"test\n",5);
	cout<<m<<endl;
	close(fd);

	fd = open("./fileIO.txt", O_CREAT|O_RDWR);
	int n = read(fd,buf,5);
	cout<<n<<endl;
	printf("%s\n",buf);
	close(fd);
	return 0;
}

So what? ?

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

using namespace std;

int main(){
    
	char* buf = new char[8];	//can't alloc 1024?
	fd = open("./fileIO.txt", O_CREAT|O_RDWR|O_APPEND);
	
	if(fd<0){
    
		cout<<"open( file failed!)"<<endl;
	}

	int m = write(fd,"test\n",5);

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

Not easy to use , That's the same sentence , I haven't saved it yet .


From this record , You can introduce a file reading and writing process to :
Open file , Get handle ;
Write the contents of the file to the buffer , At the moment, the inside of the file is empty ;
Close file , Write the contents of the new file in the buffer back to .
The next operation can be carried out .

therefore , Remember to turn it off .
also , When reading , Remember to wrap , Refresh buffer .


版权声明
本文为[Look, the future]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210808005426132m.html

随机推荐