Hello, this is my first post here. I'm writing a text file viewer for DOS using Turbo C. My code copies the contents of a text file into a buffer & displays in a text window. I can't figure out how to scroll the viewable area up and down. any ideas?
Printable View
Hello, this is my first post here. I'm writing a text file viewer for DOS using Turbo C. My code copies the contents of a text file into a buffer & displays in a text window. I can't figure out how to scroll the viewable area up and down. any ideas?
That's one little question that bugged me for quite a while. You know what? I never solved it, and have never needed to since. :(
^^^ Disclaimer: I know this post had no usefull content and I should be slapped. To avoid possible retaliation, I disown all knowledge that I ever posted it :D
Something like this?
Code:#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define length(x) ( sizeof (x) / sizeof *(x) )
enum {
KEY_ESC = 27,
ARROW_UP = 256 + 72,
ARROW_DOWN = 256 + 80,
};
static const char *text[] = {
"Line 1",
"Line 2",
"Line 3",
"Line 4",
"Line 5",
"Line 6"
};
static int get_code ( void )
{
int ch = getch();
if ( ch == 0 || ch == 224 )
ch = 256 + getch();
return ch;
}
static void redraw ( int start, int end )
{
int i;
clrscr();
for ( i = start; i < end; i++ )
puts ( text[i] );
}
int main ( void )
{
int start = 0, end = 3;
int op = 0;
while ( redraw ( start, end ), ( op = get_code() ) != KEY_ESC ) {
switch ( op ) {
case ARROW_UP:
if ( start == 0 ) {
end = length ( text );
start = end - 3;
}
else {
--start;
--end;
}
break;
case ARROW_DOWN:
if ( end == length ( text ) ) {
start = 0;
end = start + 3;
}
else {
++start;
++end;
}
break;
default: break;
}
}
return 0;
}
Thanks Prelude, that code compiled and works great! I think i can learn from it.