因为代码要在嵌入式设备上验证日志文件中的最新的n行中有我们需要的日志信息。所以用“文心一言”查了一下读取文件最后n行的方法。
考虑到嵌入式设备上内存不大行,如果文件特别大,全部加载到内存会爆内存,所以设计下面的算法。“文心一言”给的默认代码无法通过编译,本例子解决了他不能解决的问题。可能效率也不太高,还可以在优化。但是凑合用吧。
代码如下,
conanfile.txt
[requires]
nlohmann_json/3.11.3
boost/1.72.0
pybind11/2.12.0
[generators]
cmake
CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project(51_log_getter)
set(CMAKE_CXX_STANDARD 17)
add_definitions(-g)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
include_directories(${INCLUDE_DIRS} /opt/pyenv/versions/3.7.13/include/python3.7m/)
LINK_DIRECTORIES(${LINK_DIRS} /opt/pyenv/versions/3.7.13/lib/)
file( GLOB main_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file( GLOB source_files ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
foreach( main_file ${main_file_list} )
file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${main_file})
string(REPLACE ".cpp" "" file ${filename})
add_executable(${file} ${main_file} ${source_files})
target_link_libraries(${file} ${CONAN_LIBS} pthread python3.7m)
endforeach( main_file ${main_file_list})
main.cpp
#include <iostream>
#include <regex>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
std::vector<std::string> read_last_n_lines(const std::string& filename, size_t n) {
if(n == 0) {
return {};
}
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if(!file) {
std::cerr << "Could not open file: " << filename << std::endl;
return {};
}
std::streamsize size = file.tellg();
if (size == 0) {
// Empty file
return {};
}
// Read from the end of the file towards the beginning
size_t bytes_to_read = std::min<std::size_t>(size, 128); // Start with a reasonable buffer size
std::streampos file_size = file.tellg();
int start_pos = file_size;
std::string lines_buffer;
std::string cur_buffer;
while(start_pos > 0 && std::count(lines_buffer.begin(), lines_buffer.end(), '\n') < n+1) {
start_pos = start_pos - bytes_to_read;
int cur_read = bytes_to_read;
if(start_pos < 0) {
start_pos = 0;
cur_read = start_pos + bytes_to_read;
}
file.seekg(start_pos);
cur_buffer.clear();
cur_buffer.resize(cur_read);
file.read(cur_buffer.data(), cur_buffer.size());
lines_buffer = cur_buffer + lines_buffer;
}
// Remove it, just for debugging purpose
// std::cout << "current buffer: " << std::endl;
// std::cout << lines_buffer << std::endl;
// std::cout << "===================end buffer===================" << std::endl;
file.close();
std::vector<std::string> lines;
std::string line;
size_t pos = 0;
// Process the buffer to extract lines
while (pos < lines_buffer.size() && lines.size() < n + 1) {
size_t newline_pos = lines_buffer.find('\n', pos);
if (newline_pos == std::string::npos) {
// No more newlines, append the rest of the buffer as one line
line += std::string(lines_buffer.begin() + pos, lines_buffer.end());
lines.push_back(line);
break;
} else {
// Append the current line to the vector
line += std::string(lines_buffer.begin() + pos, lines_buffer.begin() + newline_pos);
lines.push_back(line);
line.clear(); // Reset line for the next iteration
pos = newline_pos + 1; // Move to the next character after the newline
}
}
if(lines.size() > 1) {
lines = std::vector<std::string>(lines.begin() + 1, lines.end());
}
return lines;
}
int main(int argc, char* argv[]) {
std::string file_name = "../planning.log";
auto lines = read_last_n_lines(file_name, 2);
for(auto const& line: lines) {
std::cout << line << std::endl;
}
std::cout << std::endl;
return EXIT_SUCCESS;
}
程序输出效果如下,