当我们射一一个断点或者慢慢履行我们的代码时,我们会想知道处于源码中的什愦地位。
打印源码
- void debugger::print_source(const std::string& file_name, unsigned line, unsigned n_lines_context) {
- std::ifstream file {file_name};
- //获得一个所需行邻近的窗口
- auto start_line = line <= n_lines_context ? 1 : line - n_lines_context;
- auto end_line = line + n_lines_context + (line < n_lines_context ? n_lines_context - line : 0) + 1;
- char c{};
- auto current_line = 1u;
- //跳过 start_line 之前的行
- while (current_line != start_line && file.get(c)) {
- if (c == '\n') {
- ++current_line;
- }
- }
- //如不雅我们在当前行则输出光标
- std::cout << (current_line==line ? "> " : " ");
- //输出行直到 end_line
- while (current_line <= end_line && file.get(c)) {
- std::cout << c;
- if (c == '\n') {
- ++current_line;
- //如不雅我们在当前行则输出光标
- std::cout << (current_line==line ? "> " : " ");
- }
- }
- //输出换行确保恰本地清空了流
推荐阅读
概述秦人不暇自哀,而后人哀之;后人哀之而不鉴之,亦使后人而复哀后人也! –论面向文档编程的重要性如不雅想看见识一小我写代码的功力,注释其实是区分老司机和小鲜肉的一个明显的分界线(有没有不雅察碘晾髑公司的>>>详细阅读
地址:http://www.17bianji.com/lsqh/36965.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示