7 Replies - 2373 Views - Last Post: 04 September 2011 - 08:05 AM Rate Topic: -----

#1 sreyan32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-December 10

Reverse line feed in console

Posted 29 August 2011 - 08:11 AM

Hello everyone,
I am writing a program in Dev C/C++ IDE which allows the user to take the input of a string without having to know the string size first.My problem is deletion (ie. handling of the backspace character). If the user enters multiple lines then the line feed cannot be reversed. If I input it on one line it is fine (see input.jpg in attachment), but if I input several lines and then the previous lines are repeated (see result.jpg in attachment) is there any way to reverse the line feed even is there a way to detect when a new line has been added. I need to know how the gets() and scanf() functions achieve this, since the same problem does not occur there and I need to know how to incorporate it in my code.The code is as following. Try it out.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
char* input();
void main(){
     printf("Enter\n");
     printf(input());
     getch();
}
/* Does not yet support character deletion.*/
char* input(){
      int i;
      char *str=NULL,ch;
      int sid=0;
      while((int)(ch=getch())!=13)
      { if(ch=='\b'&&sid>0){
      printf("\r");
      for(i=1;i<=sid;i++)
      printf("%c",' ');
      printf("\r");
      for(i=1;i<=sid-1;i++)
      printf("%c",str[i-1]);
      free(str[sid]);
      str=realloc(str,sizeof(char)*(--sid));
      }
      else{ printf("%c",ch);
      str=realloc(str,sizeof(char)*(++sid));
      str[sid-1]=ch;}
      }
      if(str==NULL)
      return NULL;
      str=realloc(str,++sid);
      str[sid-1]=NULL;
      return str;
}


Attached Image
Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Reverse line feed in console

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: Reverse line feed in console

Posted 29 August 2011 - 01:25 PM

You may need to goto to the x and y coordinates (column and row) of the screen.

how to use gotoxy()??
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Reverse line feed in console

Posted 29 August 2011 - 02:55 PM

I'm definitely old school, but I'd use conio.h 's gotoxy() or the equivalent Windows API SetConcoleCursorPosition(). Linux has Ncurses library which is about the same as conio.h.

There are also functions that will return the current row of the cursor, so those should be useful, in Windows, and Linux.
Was This Post Helpful? 0
  • +
  • -

#4 sreyan32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-December 10

Re: Reverse line feed in console

Posted 03 September 2011 - 11:41 AM

View PostAdak, on 29 August 2011 - 02:55 PM, said:

I'm definitely old school, but I'd use conio.h 's gotoxy() or the equivalent Windows API SetConcoleCursorPosition(). Linux has Ncurses library which is about the same as conio.h.

There are also functions that will return the current row of the cursor, so those should be useful, in Windows, and Linux.

Can u tell me what are the functions that return the current row and columns in Windows also the conio.h does not support gotoxy() in Dev-C.
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1635
  • View blog
  • Posts: 3,111
  • Joined: 30-May 10

Re: Reverse line feed in console

Posted 03 September 2011 - 11:51 AM

13 char *str=NULL,ch;
22 printf("%c",str[i-1]);
23 free(str[sid]);
24 str=realloc(str,sizeof(char)*(--sid));

Actually, you seriously need to work on your understanding of how memory is allocated and freed.

Frankly, if you tried this on any real operating system, the first NULL pointer access would kill the program.

Your TurboC and DOS "anything goes" just lures you into thinking it is OK, when it plainly isn't.
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Reverse line feed in console

Posted 03 September 2011 - 01:27 PM

OP said he's using DEV C, not Turbo C, Salem.

I don't understand what he means by gotoxy() is not supported in conio.h. It's part of conio.h in both Pelles C and Turbo C.

Anyway, I'm no expert on the Windows API, but i believe you can use something called GetConsoleCursorPosition in windows.h. It's a bit more complex, however:



    BOOL GetConsoleScreenBufferInfo (
    HANDLE hConsoleOutput, // handle to screen buffer
    PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo //screen buffer information
    );

    typedef struct _CONSOLE_SCREEN_BUFFER_INFO {
      COORD dwSize;
      COORD dwCursorPosition;                  
      WORD wAttributes;
      SMALL_RECT srWindow;
      COORD dwMaximumWindowSize; 
    } CONSOLE_SCREEN_BUFFER_INFO; 



Since I use the far simpler gotoxy() all the time, I can't help you much with the above. I'm sure Google has an example of it's proper usage, however.

May be easier to just count the number of newlines in the string, and keep track within the program, of how many lines of text you have that could need deleting.

This post has been edited by Adak: 03 September 2011 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#7 sreyan32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-December 10

Re: Reverse line feed in console

Posted 04 September 2011 - 07:23 AM

View PostSalem_c, on 03 September 2011 - 11:51 AM, said:

13 char *str=NULL,ch;
22 printf("%c",str[i-1]);
23 free(str[sid]);
24 str=realloc(str,sizeof(char)*(--sid));

Actually, you seriously need to work on your understanding of how memory is allocated and freed.

Frankly, if you tried this on any real operating system, the first NULL pointer access would kill the program.

Your TurboC and DOS "anything goes" just lures you into thinking it is OK, when it plainly isn't.

Thank you for the advice actually I have added the error checking in my present code, the code present here is not the latest one. I just wanted to show you guys what I was trying to accomplish. Thanks for the help.

View PostAdak, on 03 September 2011 - 01:27 PM, said:

OP said he's using DEV C, not Turbo C, Salem.

I don't understand what he means by gotoxy() is not supported in conio.h. It's part of conio.h in both Pelles C and Turbo C.

Anyway, I'm no expert on the Windows API, but i believe you can use something called GetConsoleCursorPosition in windows.h. It's a bit more complex, however:



    BOOL GetConsoleScreenBufferInfo (
    HANDLE hConsoleOutput, // handle to screen buffer
    PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo //screen buffer information
    );

    typedef struct _CONSOLE_SCREEN_BUFFER_INFO {
      COORD dwSize;
      COORD dwCursorPosition;                  
      WORD wAttributes;
      SMALL_RECT srWindow;
      COORD dwMaximumWindowSize; 
    } CONSOLE_SCREEN_BUFFER_INFO; 



Since I use the far simpler gotoxy() all the time, I can't help you much with the above. I'm sure Google has an example of it's proper usage, however.

May be easier to just count the number of newlines in the string, and keep track within the program, of how many lines of text you have that could need deleting.

What I am trying to say is that the conio.h header file present in Dev C++ 4.9.9.2 does not have the gotoxy() function, I have to implement it using the SetConsoleCursorPosition() function present in windows.h.
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Reverse line feed in console

Posted 04 September 2011 - 08:05 AM

OK, with Set... (as opposed to Get..., you'll need to keep track of where your cursor is, by counting each char you print for any one row, and also counting the rows, as well.

Here's a very small sample program in C, of how to use Set... Note that Gotoxy() is just my function name, and has NO relationship with gotoxy() in some versions of conio.h.

/* shows how to use SetConsoleCursorPosition(), in Windows */

#include <stdio.h>
#include <windows.h>

void Gotoxy(int x, int y); 
void showIt(void);

int main(void) {
  showIt();

  printf("\n\n\t\t\t    press enter when ready");
  (void) getchar();
  return 0;
}

void Gotoxy(int x, int y) {
   COORD coord;
   coord.X = x;
   coord.Y = y;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void showIt(void)  {
  short int i;
  Gotoxy(1,5);
  putchar('\n');
  for(i = 0; i < 81; i+=9)  {
     printf("  | %d%d%d | %d%d%d | %d%d%d |\n", a[i+0],a[i+1],a[i+2],a[i+3],a[i+4],a[i+5],a[i+6],a[i+7],a[i+8]);
     if(i == 18 || i == 45) printf("  +-----+-----+-----+\n");
  }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1