上のコードに、not found
と出力する処理は見当たりませんが?
スクショを撮ってここへ貼れますか?
C++ エラーで困ってます
C++でコマンドライン引数で指定されたASCII textファイルを1つ読み取り、端末(VT100)で編集するプログラムを作っています。
#include
#include
#include
#include
#include
#include
#include // for read function
using namespace std;
struct termios canonical;
struct termios raw;
void init() {
tcgetattr(0, &canonical);
raw = canonical;
cfmakeraw(&raw);
tcsetattr(0, TCSADRAIN, &raw);
}
void canonical_mode() { tcsetattr(0, TCSADRAIN, &canonical); }
void raw_mode() { tcsetattr(0, TCSADRAIN, &raw); }
void move(int row, int col) {
cout << "\033[" << row << ";" << col << "H" << flush;
}
void home() { move(1, 1); }
void clear() { cout << "\033[2J" << flush; home(); }
void done() { clear(); canonical_mode(); }
void Err() {
done();
cerr << "error" << endl;
exit(1);
}
void get_term_size(struct winsize* ws) {
if (ioctl(1, TIOCGWINSZ, ws))
Err();
}
void show(
list& buffer, list::iterator it,
string status = "q:quit, j:down, k:up, w:write, dd:delete, o:new line, 0:insert before"
) {
struct winsize ws;
get_term_size(&ws);
clear();
for (int j = 0; j < ws.ws_row - 1; j++) {
if (it == buffer.end()) break;
cout << *it << "\r\n" << flush;
it++;
}
status = status.substr(0, ws.ws_col);
status = status.append(ws.ws_col - status.size(), ' ');
move(ws.ws_row, 1);
cout << "\033[7m" << status << "\033[0m" << flush;
home();
}
int main(int argc, char* argv[]) {
init();
if (argc < 2) Err();
ifstream fin(argv[1]);
list<string> buffer;
for (string line; getline(fin, line);) buffer.push_back(line);
auto it = buffer.begin();
show(buffer, it);
while (true) {
char c;
read(0, &c, 1); // read raw input
switch (c) {
case 'q': done(); return 0;
case 'j':
if (it != buffer.end()) {
it++;
show(buffer, it);
}
break;
case 'k':
if (it != buffer.begin()) {
it--;
show(buffer, it);
}
break;
case 'w': {
ofstream fout(argv[1]);
for (const auto& line : buffer) fout << line << endl;
show(buffer, it);
break;
}
case 'd':
if (it != buffer.end()) {
it = buffer.erase(it);
show(buffer, it);
}
break;
case 'o': {
string new_line;
cin.ignore(); // ignore '\n'
getline(cin, new_line);
buffer.insert(++it, new_line);
it--;
show(buffer, it);
break;
}
case '0': {
string new_line;
cin.ignore(); // ignore '\n'
getline(cin, new_line);
buffer.insert(it, new_line);
show(buffer, it);
break;
}
default:
break;
}
}
return 0;
}
./a.outで実行しqやoを押しても
Command 'q' not foundが返ってきます。
どうすればよいでしょうか?
3Answer
Comments
追伸
<
はHTMLタグの始まり文字のため、本文に普通に書くことはできません。
プログラムコードは、以下のようにコードブロックに書きます。書き方```cpp #include <iostream> using namespace std; int main(void){ // Your code here! } ```
Comments
シェルが出しているエラーっぽいですね。
ソースコードをコードブロックに貼ってください。
#include <list>
#include <string>
#include <fstream>
#include <iostream>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h> // for read function
using namespace std;
struct termios canonical;
struct termios raw;
void init() {
tcgetattr(0, &canonical);
raw = canonical;
cfmakeraw(&raw);
tcsetattr(0, TCSADRAIN, &raw);
}
void canonical_mode() { tcsetattr(0, TCSADRAIN, &canonical); }
void raw_mode() { tcsetattr(0, TCSADRAIN, &raw); }
void move(int row, int col) {
cout << "\033[" << row << ";" << col << "H" << flush;
}
void home() { move(1, 1); }
void clear() { cout << "\033[2J" << flush; home(); }
void done() { clear(); canonical_mode(); }
void Err() {
done();
cerr << "error" << endl;
exit(1);
}
void get_term_size(struct winsize* ws) {
if (ioctl(1, TIOCGWINSZ, ws))
Err();
}
void show(
list<string>& buffer, list<string>::iterator it,
string status = "q:quit, j:down, k:up, w:write, dd:delete, o:new line, 0:insert before"
) {
struct winsize ws;
get_term_size(&ws);
clear();
for (int j = 0; j < ws.ws_row - 1; j++) {
if (it == buffer.end()) break;
cout << *it << "\r\n" << flush;
it++;
}
status = status.substr(0, ws.ws_col);
status = status.append(ws.ws_col - status.size(), ' ');
move(ws.ws_row, 1);
cout << "\033[7m" << status << "\033[0m" << flush;
home();
}
int main(int argc, char* argv[]) {
init();
if (argc < 2) Err();
ifstream fin(argv[1]);
list<string> buffer;
for (string line; getline(fin, line);) buffer.push_back(line);
auto it = buffer.begin();
show(buffer, it);
while (true) {
char c;
read(0, &c, 1); // read raw input
switch (c) {
case 'q': done(); return 0;
case 'j':
if (it != buffer.end()) {
it++;
show(buffer, it);
}
break;
case 'k':
if (it != buffer.begin()) {
it--;
show(buffer, it);
}
break;
case 'w': {
ofstream fout(argv[1]);
for (const auto& line : buffer) fout << line << endl;
show(buffer, it);
break;
}
case 'd':
if (it != buffer.end()) {
it = buffer.erase(it);
show(buffer, it);
}
break;
case 'o': {
string new_line;
cin.ignore(); // ignore '\n'
getline(cin, new_line);
buffer.insert(++it, new_line);
it--;
show(buffer, it);
break;
}
case '0': {
string new_line;
cin.ignore(); // ignore '\n'
getline(cin, new_line);
buffer.insert(it, new_line);
show(buffer, it);
break;
}
default:
break;
}
}
return 0;
}
お願いします