conio.h - portability issues

I've been programming a command line 'text editor'
The problem I'm facing is that, I want to make the implementation OS independent. That is, I'd like my code to work in both Linux and Windows. But the problem is, I can't use 'conio.h' as it is OS dependent. I've searched the web and found out there is no alternative for this header file (which is OS independent) and there is neither a similar one in linux.
I'm finding hard without this header file but I'm determined not to use it...
please help...

PS: some of the methods in conio.h which i was using previously were
1
2
3
4
5
int getch(void);   //unbuffered input and doesn't echo on screen
int getche(void);  //unbuffered input but echos on screen
void gotoxy(int x, int y);   //moves the cursor to the coordinates specified
int wherex(void);    //returns the x coordinate of the cursor's position
int wherey(void);    //returns the y coordinate of the cursor's position 


Thanks in advance! :)
If you need that functionality, I know of no way to do that portably. You need to have OS-specific code to do that (using curses on Linux/Unix).
curses libraries are cross-platform
Interesting. Thanks Bazzy and Albatross.
you also can use this header file for linux systems and conio.h for windows systems.. just write some macro lines so that it chooses the correct system and define the header part... this is what i used for listening to keyboard input with the termios.h for linux...
i dont know if this is the best way for you... i hope this helps...
check out that ncurses library too... its maybe better i guess...
cheers..

p.s.
this code is not mine, i found it on the internet and modified it..
thank the maker

linuxconio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* fileno setbuf stdin */
#include <stdio.h>

/* NULL */
#include <stddef.h>

/* termios tcsetattr tcgetattr TCSANOW */
#include <termios.h>

/* ioctl FIONREAD ICANON ECHO */
#include <sys/ioctl.h>

#include <unistd.h>

static int initialized = 0;
static struct termios original_tty;

void kbinit();
void kbfini();
int kbhit();

int kbhit() 
{
  if(!initialized)
  {
    kbinit();
  }

  int bytesWaiting;
  ioctl(fileno(stdin), FIONREAD, &bytesWaiting);
  return bytesWaiting;
}

/* Call this just when main() does its initialization. */
/* Note: kbhit will call this if it hasn't been done yet. */
void kbinit()
{
  struct termios tty;
  tcgetattr(fileno(stdin), &original_tty);
  tty = original_tty;

  /* Disable ICANON line buffering, and ECHO. */
  tty.c_lflag &= ~ICANON;
  tty.c_lflag &= ~ECHO;
  tcsetattr(fileno(stdin), TCSANOW, &tty);

  /* Decouple the FILE*'s internal buffer. */
  /* Rely on the OS buffer, probably 8192 bytes. */
  setbuf(stdin, NULL);
  initialized = 1;
}

/* Call this just before main() quits, to restore TTY settings! */
void kbfini()
{
  if(initialized)
  {
    tcsetattr(fileno(stdin), TCSANOW, &original_tty);
    initialized = 0;
  }
}


Topic archived. No new replies allowed.