如何使用Windows conio.h将代码移植到Linux?
问题描述:
我写了这个C程序的Win32/C编译器,但是当我试图在Linux机器或codepad.org使用gcc运行这个它显示'conio.h:没有这样的文件或目录编译终止'什么是修改要做不包括任何其他新执行该程序包括像curses.h如何使用Windows conio.h将代码移植到Linux?
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j=0,k=0,n,u=0;
char s[100],c2,c[10];
char c1[3]={'a','b','c'};
clrscr();
printf("no of test cases:");
scanf("%d",&n);
for(u=0;u<n;u++)
{
printf("Enter the string:");
scanf("%s",s);
i=0;
while(s[i]!='\0')
{
if(s[i+1]=='\0')
break;
if(s[i]!=s[i+1])
{
for(j=0;j<3;j++)
{
if((s[i]!=c1[j])&&(s[i+1]!=c1[j]))
{
c2=c1[j];
}
}
s[i]=c2;
for(k=i+1;k<100;k++)
{
s[k]=s[k+1];
}
i=0;
}
else
i++;
}
c[u]=strlen(s);
}
for(u=0;u<n;u++)
printf("%d\n",c[u]);
getch();
}
答
我没有看你的代码,看看它是否需要这三个函数。但这是获得它们的简单方法。通常比使用getch()更好。清除我的屏幕时,clrscr()也没什么好玩的!
#include<stdio.h>
#include <stdlib.h> // system
#include <string.h> // strlen
#include <termios.h> // getch
#include <unistd.h> // getch
void clrscr()
{
// Works on systems that have clear installed in PATH
// I don't like people clearing my screen though
system("clear");
}
int getch()
{
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
答
它看起来像你从conio.h
使用的唯一功能是clrscr()
和getch()
。只要把它们拿出来,你应该没问题 - 它们似乎不会影响程序的运行。他们在这里使用更像是Windows终端行为的解决方法。
有两点要注意:
-
main()
应该返回int
。 -
strlen()
在string.h
中定义 - 您可能会想要包括这一点。
答
回顾你的问题我可以看到clrscr()和getch()你使用的是conio.h但是这个头文件在gcc中不可用。因此,对于clrscr使用
system("clear");
,正如你所提到的残培()使用的library
干杯!
+0
诅咒是矫枉过正。 'gets()'会做得很好。 –
无残培()程序超时.. – Vilva