在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。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:C++在Linux中如何进行文件操作