0

I am pretty new to parallel programming. In my C program, I need to display my statistical data on the terminal which is different than the main one.

I mean:

I am implementing a simple text editor. While user writing a data, I hold the letter size by the help of a thread. I want to display the letter size at any moment, however it is not possible since user is interrupted while typing. Therefore, I am thinking of opening a new command prompt window and display that letter size information there.

How can I do that? I use g++ compiler in Ubuntu.

Thanks

skynyrd
  • 942
  • 4
  • 14
  • 34
  • What have you tried? How did it not work? Do you think you want to have two independent threads running? Does it have to be a different window? – Floris Apr 16 '13 at 01:55
  • I can already get an information about the length of the string. User starts to write something on the command prompt (I used while looped getchar() function). When new char is fetched from user, thread is activated and increment the # of the characters. The problem is that: I want to display the character size while user is typing. I can print the data to the screen but it just places when user is typing. So I want to use different window.. (Actually it is totally same idea with this comment box, ## characters left is written when I am writing). – skynyrd Apr 16 '13 at 02:24

1 Answers1

0

I don't think you need to spawn a new shell to display the line. If what you want is like the comment box, give this a try ( N.B: I have not implemented any escape character, so backspace and escape won't work)

Had a little help from here What is Equivalent to getch() & getche() in Linux?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 255
#ifdef _WIN32
#include <conio.h>
#else
#include <termios.h>

static struct termios old, newT;

/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
    tcgetattr(0, &old); /* grab old terminal i/o settings */
    newT = old; /* make new settings same as old settings */
    newT.c_lflag &= ~ICANON; /* disable buffered i/o */
    newT.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
    tcsetattr(0, TCSANOW, &newT); /* use these new terminal i/o settings now */
}

/* Restore old terminal i/o settings */
void resetTermios(void)
{
    tcsetattr(0, TCSANOW, &old);
}

/* Read 1 character - echo defines echo mode */
char getch(int echo)
{
    char ch;
    initTermios(echo);
    ch = getchar();
    resetTermios();
  return ch;
}
#endif // _WIN32


int main()
{
    char arrayInput[MAXLEN + 1];
    static char printArray[MAXLEN + 15];
    static char tmp[5];
    char c;
    int i,len=0;
    printf("Begin\n");
    for(i=0;i<MAXLEN;i++)
    {
        putchar('\r');
        c = getch();
        for(len=0;len<MAXLEN;len++)
            putchar(' ');
        putchar('\r');
        arrayInput[i]=c;
        arrayInput[i+1] = '\0';
        strcpy(printArray,"");
        strcpy(printArray,arrayInput);
        strcat(printArray," Size is:: ");
        sprintf(tmp,"%d",i+1);
        strcat(printArray,tmp);
        printf("%s",printArray);
    }
    return 0;
}
Community
  • 1
  • 1