1

I want to know if there is any way to copy content of a line from console window into string. For example

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 int main()
 {
  char a[10];int b;  int x1,y1,x2,y2,x3,y3;

  x1=wherex(); y1=wherey();
  printf("Enter your name : ");
  scanf("%s",&a);
  x2=wherex(); y3=wherey();
  printf("Enter your age : ");
  scanf("%d",&b);
  x3=wherex(); y3=wherey();
  printf("Enter your gender : ");
  scanf("%d",&a);

  char copyline1[80];char copyline2[80];char copyline3[80];
  gotoxy(x1,y1);
  copyline1[]= ??? // What to do to Copy content of line 1 into copyline1[]
  gotoxy(x2,y2);
  copyline2[]= ??? // What to do to Copy content of line 2 into copyline2[]
  gotoxy(x3,y3);
  copyline3[]= ??? // What to do to Copy content of line 3 into copyline3[]

  printf(" First line is\n");
  puts(copyline1);  // This will print Enter your name : abc
  printf(" Second line is\n");
  puts(copyline2);  // This will print content of line 2 of console window
  printf(" Third line is \n");
  puts(copyline3);

  return 0;
 }

I want to copy content of line into string by giving line's coordinates , if possible !

udit043
  • 1,610
  • 3
  • 22
  • 40
  • There's no way to read text from the screen, unless you're interacting with the terminal directly. – πάντα ῥεῖ Jun 13 '15 at 10:05
  • Please choose C or C++, the answer will not be the same for both. – Baum mit Augen Jun 13 '15 at 10:12
  • There is no Standard C solution. If you really want to give it a go, include your Operating System, as an answer is going to *entirely* depend on that. In Windows and plain MS-DOS, for example, it is possible (and it's two different solutions). – Jongware Jun 13 '15 at 10:39
  • [ReadConsoleOutputCharacter](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684969(v=vs.85).aspx) – BLUEPIXY Jun 13 '15 at 10:42
  • For C , i would like to know the answer @BaummitAugen – udit043 Jun 13 '15 at 11:46
  • BLUEPIXY gave the answer for Windows. If you read the link, you will see it wants to know the *number of characters*; and of course, there is no such thing as a "string" when text has been printed on the console. You cannot know where the string ends. If the text ends at the very rightmost column, it may have been wrapped to the next line. – Jongware Jun 13 '15 at 14:13

1 Answers1

0
    char buf[70];
    COORD coord = {x-coordinate,y-coordinate};
    DWORD num_read;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    ReadConsoleOutputCharacter(hConsole,(LPTSTR) buf,(DWORD) 70,coord,(LPDWORD) &num_read);
    buf[69]='\0';

Content of the line is now copied into char buf.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70