C++基于LINUX的文件操作
引言
討論套接字的過程討論突然提及文件也許有些奇怪。但對(duì)于LINUX而言,socket操作和文件操作沒有區(qū)別,Linux一切皆為文件,因此文件的IO函數(shù)也是socket的IO函數(shù),本文旨在給讀者擴(kuò)充知識(shí),不必記住所謂的代碼
底層文件訪問和文件描述符
- 底層:與標(biāo)準(zhǔn)無關(guān)底層提供的函數(shù)
文件描述符:系統(tǒng)分配給文件或者套接字的整數(shù),windows被稱為句柄,用來描述一種時(shí)間類型或者事務(wù)。
打開文件
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> int open(const char* path, int flag)
關(guān)閉文件
#include<unistd.h> int close(int fd);
fd->需要關(guān)閉的文件或者套接字的文件描述符
將數(shù)據(jù)寫入文件
#include<unistd.h> ssize_t write(int fd, const void* buf, size_t nbytes);
fd顯示數(shù)據(jù)傳輸對(duì)象的文件描述符。
將數(shù)據(jù)寫入文件
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> void error_handling(char* message); int main(void) { int fd; char buf[]="Let's go!\n"; fd=open("data.txt", O_CREAT|O_WRONLY|O_TRUNC); if(fd==-1) error_handling("open() error!"); printf("file descriptor: %d \n", fd); if(write(fd, buf, sizeof(buf))==-1) error_handling("write() error!"); close(fd); return 0; } void error_handling(char* message) { fputs(message, stderr); fputc('\n', stderr); exit(1); } /* root@com:/home/swyoon/tcpip# gcc low_open.c -o lopen root@com:/home/swyoon/tcpip# ./lopen file descriptor: 3 root@com:/home/swyoon/tcpip# cat data.txt Let's go! root@com:/home/swyoon/tcpip# */
讀取文件中的數(shù)據(jù)
#include <unistd.h> ssize_t read(int fd, void* buf, size_t nbytes);
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #define BUF_SIZE 100 void error_handling(char* message); int main(void) { int fd; char buf[BUF_SIZE]; fd=open("data.txt", O_RDONLY); if( fd==-1) error_handling("open() error!"); printf("file descriptor: %d \n" , fd); if(read(fd, buf, sizeof(buf))==-1) error_handling("read() error!"); printf("file data: %s", buf); close(fd); return 0; } void error_handling(char* message) { fputs(message, stderr); fputc('\n', stderr); exit(1); } /* root@com:/home/swyoon/tcpip# gcc low_read.c -o lread root@com:/home/swyoon/tcpip# ./lread file descriptor: 3 file data: Let's go! root@com:/home/swyoon/tcpip# */
文件描述符與套接字
下面將同時(shí)創(chuàng)建文件和套接字,并用整數(shù)形態(tài)比較返回的文件描述符值。
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/socket.h> int main(void) { int fd1, fd2, fd3; fd1=socket(PF_INET, SOCK_STREAM, 0); fd2=open("test.dat", O_CREAT|O_WRONLY|O_TRUNC); fd3=socket(PF_INET, SOCK_DGRAM, 0); printf("file descriptor 1: %d\n", fd1); printf("file descriptor 2: %d\n", fd2); printf("file descriptor 3: %d\n", fd3); close(fd1); close(fd2); close(fd3); return 0; }
以上就是C++基于LINUX的文件操作的詳細(xì)內(nèi)容,更多關(guān)于C++ LINUX文件操作的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解C語言中strcpy()函數(shù)與strncpy()函數(shù)的使用
這篇文章主要介紹了詳解C語言中strcpy()函數(shù)與strncpy()函數(shù)的使用,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08深入探究協(xié)程在C++中的實(shí)現(xiàn)方式
協(xié)程可以被看作是計(jì)算機(jī)程序中的獨(dú)立功能塊,它們在執(zhí)行過程中能夠暫停和恢復(fù),與傳統(tǒng)的函數(shù)調(diào)用相比,協(xié)程更像是一種輕量級(jí)的線程,本文我們將深入探究協(xié)程在C++中的實(shí)現(xiàn)方式,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-12-12C++實(shí)現(xiàn)銀行排隊(duì)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)銀行排隊(duì)系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07C++實(shí)現(xiàn)循環(huán)隊(duì)列
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)循環(huán)隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01C++實(shí)現(xiàn)JPEG格式圖片解析(附代碼)
這篇文章主要為大家詳細(xì)介紹了C++如何實(shí)現(xiàn)JPEG格式圖片解析功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-05-05