所以我编写了以下简单程序,询问用户输入的内容,然后询问是否要再次执行此操作或退出。
Document document = null;
Element documentElement = null;
final DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
Element root = null;
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
root = document.createElement("kml");
document.appendChild(root);
documentElement = document.createElement("Document");
final Element topNameElement = document.createElement("name");
documentElement.appendChild(topNameElement);
root.appendChild(documentElement);
root.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance");
root.setAttribute("xmlns", "http://schemas.opengis.net/kml/2.2.0");
root.setAttribute("xmlns:ar", "http://schemas.opengis.net/arml/2.0");
root.setAttribute("xsi:schemaLocation", "http://schemas.opengis.net/kml/2.2.0 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://schemas.opengis.net/arml/2.0 http://schemas.opengis.net/arml/2.0/arml.xsd");
该程序在Windows下使用mingw64编译运行正常,但是在将conio.h更改为ncurses.h并在linux下编译时,行为会发生变化。程序运行<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://schemas.opengis.net/kml/2.2.0" xmlns:ar="http://schemas.opengis.net/arml/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.opengis.net/kml/2.2.0 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd 	http://schemas.opengis.net/arml/2.0 	http://schemas.opengis.net/arml/2.0/arml.xsd">
<kml>
</kml>
函数很好但在用户点击'ENTER'后,#include <conio.h> /* or use ncurses for getch() */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
void ReadConsoleInput(void) {
char buffer[83];
char *result;
printf("\nInput line of text, followed by carriage return:\n");
result = fgets(buffer, sizeof buffer, stdin);
buffer[strcspn(result, "\r\n")] = '\0';
if (!result) {
printf(
"An error occurred reading from the console:"
" error code %d\n",
errno);
} else {
printf("\nLine length = %d\nText = %s\n", (int)strlen(result), result);
}
}
int Repeat() {
printf("Again? (Y/N): \n");
int ch;
ch = getch();
return (ch == 'Y' || ch == 'y') ? TRUE : FALSE;
}
int main() {
do { /* infinite loop */
ReadConsoleInput();
} while (Repeat());
return 0;
}
函数只显示,程序结束,然后用户可以输入任何字符。
linux下发生了什么不同?
(另外,如果无限循环不是我正在尝试做的正确技术,请建议。我觉得大多数程序都不依赖无限循环测试来保持开放。)
最终我计划将程序扩展为使用树数据结构存储文本并从那里学习,但我希望保持跨平台的统一性。
答案 0 :(得分:2)
继续我的评论,完全摆脱conio.h
。除了windoze / DOS之外,它是100%不可移植的。您可以使用getchar
代替getch
,只需考虑用户输入'\n'
后剩余的输入缓冲区中剩余的Y/N
。例如,您可以创建类似于以下内容的可移植实现:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define TRUE 1
#define FALSE 0
void ReadConsoleInput(void) {
char buffer[83];
char *result;
printf("\nInput line of text, followed by carriage return:\n");
result = fgets(buffer, sizeof buffer, stdin);
if (!result) {
printf(
"An error occurred reading from the console:"
" error code %d\n",
errno);
} else {
buffer[strcspn(result, "\r\n")] = '\0';
printf("\nLine length = %d\nText = %s\n", (int)strlen(result), result);
}
}
int Repeat() {
printf("Again? (Y/N): \n");
int c, ch;
ch = getchar();
/* empty input buffer */
for (c = getchar(); c != '\n' && c != EOF; c = getchar()) {}
return (ch == 'Y' || ch == 'y') ? TRUE : FALSE;
}
int main (void) {
do { /* infinite loop */
ReadConsoleInput();
} while (Repeat());
return 0;
}
(在调用ch
清空缓冲区之前,您需要检查EOF
是否为getchar
并相应处理以捕获用户取消输入)
示例使用/输出
$ ./bin/noconio
Input line of text, followed by carriage return:
a quick brown fox
Line length = 17
Text = a quick brown fox
Again? (Y/N):
y
Input line of text, followed by carriage return:
jumps over a lazy dog
Line length = 21
Text = jumps over a lazy dog
Again? (Y/N):
n
仔细看看,如果这是你的意图,请告诉我。