-2
我想没有一些PARAMS营造出宛如more
用于Linux的程序。主要思想是我需要从命令字符串中的某些参数输出文本文件中的信息。所以我的主要观点是more -d fileName
。控制台尺寸++
我知道more
输出23串文字,和第24的是,用户按space
和获得信息的另一个屏幕,但我需要考虑的是用户可以更改控制台窗口的大小。
我想使用的库#include <sys/ioctl.h>
,但它说,我不能使用这样的库。我究竟做错了什么?
我的代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sys/ioctl.h>
void printRecord(struct winsize w, std::vector<std::string> lines);
int main(int argc, char *argv[]) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
std::ifstream readRecord;
std::string more("more");
std::vector<std::string> lines;
std::cout << "argc: " << argc << std::endl;
for (int i = 0; i < argc; i++)
{
std::cout << "Argument: " << i << " = " << argv[i] << std::endl;
}
std::cout << std::endl << std::endl;
if (argv[1] == more)
{
std::string str;
//int n = atoi(argv[2]);
int numberPages=0;
readRecord.open(argv[2]);
while (!readRecord.eof())
{
getline(readRecord, str);
numberPages++;
lines.push_back(str);
}
if (0 == numberPages)
{
std::cout << "ERROR: The file is empty" << std::endl;
exit(-1);
}
if (w.ws_row > numberPages)
{
printRecord(struct winsize w, lines);
}
else
{
printRecord(struct winsize w, lines);
}
}
return 0;
}
void printRecord(struct winsize w,std::vector<std::string> lines)
{
for (int i = 0; i < w.winsize::ws_row; i++)
{
std::cout << lines[i] << std::endl;
}
}
错误:
Source.cpp: In function ‘int main(int, char**)’: Source.cpp:12:11: error: ‘STDOUT_FILENO’ was not declared in this scope ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); Source.cpp:44:16: error: expected primary-expression before ‘struct’ printRecord(struct winsize w, lines); Source.cpp:48:16: error: expected primary-expression before ‘struct’ printRecord(struct winsize w, lines);
“但它说,我不能用这样的库” - 谁这么说?你有编译器错误吗?你也可以看看它是如何在'less'程序的linux下的源代码,这是可以[点击这里]完成(http://www.greenwoodsoftware.com/less/)。 – Axel
@Axel是的,我有一个编译器错误。感谢您的支持! –
@NikitaGusev:我们不是通灵,你真的应该包括错误。 – MSalters