在Linux下,C++可以通過多種方式進行高效的文件操作。以下是一些常用的方法和技巧:
<fstream>
C++標準庫提供了<fstream>
頭文件,其中包含了文件流類,如ifstream
、ofstream
和fstream
,用于文件的讀寫操作。
#include <fstream>
#include <iostream>
int main() {
// 寫文件
std::ofstream outFile("example.txt");
if (outFile.is_open()) {
outFile << "Hello, World!" << std::endl;
outFile.close();
} else {
std::cerr << "Unable to open file for writing." << std::endl;
}
// 讀文件
std::ifstream inFile("example.txt");
if (inFile.is_open()) {
std::string line;
while (std::getline(inFile, line)) {
std::cout << line << std::endl;
}
inFile.close();
} else {
std::cerr << "Unable to open file for reading." << std::endl;
}
return 0;
}
對于更高效的文件操作,可以使用POSIX標準的低級I/O函數,如open
、read
、write
和close
。
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
int main() {
// 打開文件
int fd = open("example.txt", O_RDWR | O_CREAT, 0666);
if (fd == -1) {
std::cerr << "Unable to open file." << std::endl;
return 1;
}
// 寫文件
const char* message = "Hello, World!\n";
ssize_t bytesWritten = write(fd, message, strlen(message));
if (bytesWritten == -1) {
std::cerr << "Error writing to file." << std::endl;
}
// 關閉文件
close(fd);
// 重新打開文件以讀取
fd = open("example.txt", O_RDONLY);
if (fd == -1) {
std::cerr << "Unable to open file for reading." << std::endl;
return 1;
}
// 讀文件
char buffer[1024];
ssize_t bytesRead = read(fd, buffer, sizeof(buffer) - 1);
if (bytesRead > 0) {
buffer[bytesRead] = '\0'; // Null-terminate the string
std::cout << buffer;
}
// 關閉文件
close(fd);
return 0;
}
內存映射文件是一種高效的文件讀寫方法,它允許將文件的一部分或全部映射到進程的地址空間中。
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
int main() {
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
std::cerr << "Unable to open file." << std::endl;
return 1;
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
std::cerr << "Error getting file size." << std::endl;
close(fd);
return 1;
}
char* addr = static_cast<char*>(mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0));
if (addr == MAP_FAILED) {
std::cerr << "Error mapping file." << std::endl;
close(fd);
return 1;
}
std::cout << addr;
if (munmap(addr, sb.st_size) == -1) {
std::cerr << "Error unmapping file." << std::endl;
}
close(fd);
return 0;
}
Linux提供了異步I/O接口(如aio
庫),可以在不阻塞主線程的情況下進行文件操作。
#include <aio.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
int main() {
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
std::cerr << "Unable to open file." << std::endl;
return 1;
}
char buffer[1024];
struct aiocb cb;
memset(&cb, 0, sizeof(cb));
cb.aio_fildes = fd;
cb.aio_nbytes = sizeof(buffer);
cb.aio_buf = buffer;
cb.aio_offset = 0;
if (aio_read(&cb) == -1) {
std::cerr << "Error starting async read." << std::endl;
close(fd);
return 1;
}
// Do other work while the read is in progress
while (aio_error(&cb) == EINPROGRESS) {
// Wait for the read to complete
}
ssize_t bytesRead = aio_return(&cb);
if (bytesRead > 0) {
buffer[bytesRead] = '\0'; // Null-terminate the string
std::cout << buffer;
}
close(fd);
return 0;
}
選擇哪種方法取決于具體的應用場景和性能需求。對于大多數應用,標準庫的<fstream>
已經足夠高效。如果需要更高的性能,可以考慮使用低級I/O函數、內存映射文件或異步I/O。