我正在尝试创建一个类似于moreLinux 的程序,但没有一些参数。主要思想是我需要从文本文件中输出信息,并在命令字符串中包含一些参数。所以我的主要论点是more -d fileName。
我知道more输出 23 个带有文本的字符串,第 24 个是用户按下的space并获取另一个信息屏幕,但我需要考虑到用户可以更改控制台窗口的大小。
我正在尝试使用图书馆#include <sys/ioctl.h>,但它说我不能使用这样的库。我究竟做错了什么?
#include <iostream>\n#include <fstream>\n#include <vector>\n#include <string>\n#include <sys/ioctl.h>\n\nvoid printRecord(struct winsize w, std::vector<std::string> lines);\n\nint main(int argc, char *argv[]) {\n struct winsize w;\n ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);\n std::ifstream readRecord;\n std::string more("more");\n std::vector<std::string> lines;\n\n std::cout << "argc: " << argc << std::endl;\n for (int i = 0; i < argc; i++)\n {\n std::cout << "Argument: " << i << " = " << argv[i] << std::endl;\n }\n std::cout << std::endl << std::endl;\n\n if (argv[1] == more)\n {\n std::string str;\n //int n = atoi(argv[2]);\n int numberPages=0;\n readRecord.open(argv[2]);\n\n while (!readRecord.eof())\n {\n getline(readRecord, str);\n numberPages++;\n lines.push_back(str);\n }\n if (0 == numberPages)\n {\n std::cout << "ERROR: The file is empty" << std::endl;\n exit(-1);\n }\n if (w.ws_row > numberPages)\n {\n printRecord(struct winsize w, lines);\n }\n else\n {\n printRecord(struct winsize w, lines);\n }\n }\n\n return 0;\n}\nvoid printRecord(struct winsize w,std::vector<std::string> lines)\n{\n for (int i = 0; i < w.winsize::ws_row; i++)\n {\n std::cout << lines[i] << std::endl;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\nRun Code Online (Sandbox Code Playgroud)\n#include <iostream>\n#include <fstream>\n#include <vector>\n#include <string>\n#include <sys/ioctl.h>\n\nvoid printRecord(struct winsize w, std::vector<std::string> lines);\n\nint main(int argc, char *argv[]) {\n struct winsize w;\n ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);\n std::ifstream readRecord;\n std::string more("more");\n std::vector<std::string> lines;\n\n std::cout << "argc: " << argc << std::endl;\n for (int i = 0; i < argc; i++)\n {\n std::cout << "Argument: " << i << " = " << argv[i] << std::endl;\n }\n std::cout << std::endl << std::endl;\n\n if (argv[1] == more)\n {\n std::string str;\n //int n = atoi(argv[2]);\n int numberPages=0;\n readRecord.open(argv[2]);\n\n while (!readRecord.eof())\n {\n getline(readRecord, str);\n numberPages++;\n lines.push_back(str);\n }\n if (0 == numberPages)\n {\n std::cout << "ERROR: The file is empty" << std::endl;\n exit(-1);\n }\n if (w.ws_row > numberPages)\n {\n printRecord(struct winsize w, lines);\n }\n else\n {\n printRecord(struct winsize w, lines);\n }\n }\n\n return 0;\n}\nvoid printRecord(struct winsize w,std::vector<std::string> lines)\n{\n for (int i = 0; i < w.winsize::ws_row; i++)\n {\n std::cout << lines[i] << std::endl;\n }\n}\n
问题是你用过
#include <sys/ioct1.h>
Run Code Online (Sandbox Code Playgroud)
并且有一个拼写错误:1而不是l。尝试使用:
#include <sys/ioctl.h>
Run Code Online (Sandbox Code Playgroud)
反而