How to port code using Windows conio.h to Linux?

1.4k views Asked by At

I wrote this C program for Win32/c compiler but while i'm trying run this using gcc in Linux machine or codepad.org it shows 'conio.h: No such file or directory compilation terminated' What are modification to be done to execute this program without including any other new includes like 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

There are 3 answers

0
Joe McGrath On BEST ANSWER

I did not look at your code to see if it needs the three functions. But this is the simplist way to get them. There is normaly a better way than using getch(). clrscr() is also no fun when you clear my screen !

#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()

0
Carl Norum On

It looks like the only functions you're using from conio.h are clrscr() and getch(). Just take those out and you should be fine - they don't appear to affect the operation of the program. They're being used here more like workarounds for windows terminal behaviour.

A couple of notes:

  1. main() should return int.
  2. strlen() is defined in string.h - you'll probably want to include that.
1
Alim Ul Gias On

Reviewing your question I can see that for clrscr() and getch() you are using conio.h But this header is not available in gcc. So for clrscr use

system("clear");

and as you have mentioned for getch() use the curses library

Cheers !!