如何使用Windows conio.h将代码移植到Linux?

时间:2011-11-06 05:46:31

标签: c include conio

我为Win32 / c编译器编写了这个C程序,但是我试图在Linux机器或codepad.org中使用gcc运行它,它显示' conio.h:没有这样的文件或目录编译终止&# 39;如果不包括任何其他新的包括curses.h

,那么执行此程序需要做哪些修改
#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();
}

3 个答案:

答案 0 :(得分:3)

您在conio.h中使用的唯一功能似乎是clrscr()getch()。把它们拿出去你应该没问题 - 它们似乎不会影响程序的运行。它们在这里的使用更像是Windows终端行为的解决方法。

几点说明:

  1. main()应该返回int
  2. strlen()string.h中定义 - 您可能希望包含该内容。

答案 1 :(得分:2)

回顾你的问题我可以看到对于clrscr()和getch()你正在使用conio.h但是这个头文件在gcc中不可用。所以对于clrscr使用

system("clear");

正如您在getch()中提到的那样使用curses库

干杯!!

答案 2 :(得分:0)

我没有查看你的代码,看它是否需要这三个函数。但这是获得它们的简单方法。有一种比使用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;
}

getch()