C 中的字符串操作?
我想打印一个字符数组,这些字符首先是下划线。 然后用户可以在这些下划线上写入字符。我使用了gotoxy(),但它不能正常工作。 这就是我写的:
int main(void)
{
char arr[20];
int i;
char ch;
clrscr();
for(i=0;i<=20;i++)
{
textattr(0x07);
cprintf("_");
}
do
{
for(i=0;i<=20;i++)
{
//gotoxy(i,0);
//ch = getche();
if( isprint(ch) == 1)
{
arr[i] = ch;
gotoxy(i,0);
//printf("%c",ch);
}
}
} while(i == 20);
getch();
return 0;
}
I want to print an array of characters, these characters are underscores first.
Then the user can write characters on these underscores.I used gotoxy() but it doesn't work properly.
That is what i wrote:
int main(void)
{
char arr[20];
int i;
char ch;
clrscr();
for(i=0;i<=20;i++)
{
textattr(0x07);
cprintf("_");
}
do
{
for(i=0;i<=20;i++)
{
//gotoxy(i,0);
//ch = getche();
if( isprint(ch) == 1)
{
arr[i] = ch;
gotoxy(i,0);
//printf("%c",ch);
}
}
} while(i == 20);
getch();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一件事是:您可能不希望在主函数中调用所有这些对
gotoxy
、textattr
和cprintf
的调用,因为这不是 main 函数应该做的。主函数的目的更有可能是“从用户那里读取一些文本,并在输入字段中很好地呈现”。所以你应该把它变成一个函数:
The first thing is this: You probably don't want to have all those calls to
gotoxy
,textattr
andcprintf
in your main function, since that is not what the main function is supposed to do.It is much more likely that the main function's purpose is "to read some text from the user, presented nicely in an input field". So you should make this a function:
打印字符数组相当容易:
从内存中将字符串打印到屏幕上。
Printing an array of characters is fairly easy:
From memory that should print a string out to the screen.
您的代码非常特定于 DOS。对于以可移植的方式读取立即输入的问题,没有一个好的通用解决方案。它确实经常被问到,所以我认为 C 常见问题解答已分解并包含您可能想要寻找的答案。
也就是说,我认为你的错误是
gotoxy(1, 1)
是屏幕的上角,而不是 0,0。所以你想要gotoxy(i, 1)
Your code is very DOS-specific. There is not a good general solution to the problem of reading immediate input in a portable way. It does get asked quite often, so I think the C FAQ broke down and included an answer which you might want to seek out.
That said, I think your bug is that
gotoxy(1, 1)
is the upper corner of the screen, not 0,0. So you wantgotoxy(i, 1)