LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   accept input escape new line (http://www.linuxquestions.org/questions/programming-9/accept-input-escape-new-line-302800/)

monil 03-17-2005 12:57 PM

accept input escape new line
 
Hi,

Is it possible to accept huge iinput of text ? including the <return> keys, enter pressed multiple times? Then, is it possible to have the text inputted to be quit when the user presses a combination of cntrl + x or something?

I would appreciate some sample code in this regards....

Thank you.

-Monil

TheLinuxDuck 03-17-2005 01:02 PM

What language?

monil 03-17-2005 01:14 PM

Sorry...... I need some sample code in C...

Thnx

bullium 03-17-2005 01:14 PM

writing your own keylogger? :D

monil 03-17-2005 01:18 PM

nah.. (I wish I could write one :-) )
I want to accept inputs like a paragraph or something and write it to disk blok...

ksgill 03-17-2005 01:24 PM

Yes, it is. You can do it with standard in.. it keeps accepting input until Ctrl +D (?) is pushed in unix. Or the other way is to create a file from the input and then later on open that file and read from it. I wrote something like that last year for school.. not sure if I have it anymore :(

itsme86 03-17-2005 01:30 PM

It's tricky to get key combinations like CTRL-x usually. You can catch CTRL-d which sends the program EOF (CTRL-z) under Windows. Something like this seems to work:
Code:

itsme@dreams:~/C$ cat log.c
#include <stdio.h>
#include <unistd.h>

int main(void)
{
  FILE *fp;
  char ch;

  if(!(fp = fopen("dump.log", "w")))
  {
    puts("Couldn't open 'dump.log' for writing!");
    return 1;
  }

  while(read(fileno(stdin), &ch, 1) == 1)
    fputc(ch, fp);
  fclose(fp);

  return 0;
}

Code:

itsme@dreams:~/C$ ./log
this
is a
multiline
test
itsme@dreams:~/C$ cat dump.log
this
is a
multiline
test
itsme@dreams:~/C$

Just press CTRL-d when done typing. You could also just use something like getchar() instead of read() I guess :)

monil 03-18-2005 10:23 AM

Code:

#include <stdio.h>
#include <unistd.h>
#include <malloc.h>

int main(void)
{
  char ch[2];
  char *str = malloc(1024);
int i=0;
sprintf(str,"");
  while(read(fileno(stdin), &ch[0], 1) == 1)
{
    strcat(str, ch);
}

for(i=0; i<10; i++)
        printf("%c", str[i]);
 return 0;
}

I am unable to concatenate the input in a string.. Am i doing ny thing wrong?

Pls help

I am wanting to accept input from the keyboard and then, store it in a string. IT shld be able to take multiple lines...

Thank you

-Monil

itsme86 03-18-2005 10:38 AM

An updated version of my code that should do it:
Code:

#include <stdio.h>
#include <unistd.h>

int main(void)
{
  FILE *fp;
  char ch;
  char str[1024];
  char *s = str;

  if(!(fp = fopen("dump.log", "w")))
  {
    puts("Couldn't open 'dump.log' for writing!");
    return 1;
  }

  while(read(fileno(stdin), &ch, 1) == 1)
    *s++ = ch;
  *s = '\0';

  fclose(fp);

  return 0;
}


monil 03-18-2005 12:49 PM

Thnx.. BUt, I cant seem to print the contents of the string 's'... Am i missing something?

-Monil

ksgill 03-18-2005 12:50 PM

You should open the file *fp and everythin should be in there. 's' is just a character that is being written to the file.

monil 03-18-2005 12:53 PM

I am interested in storing in the string directly and then directly pass it to a function that I have.... So, can u tell where i am missing?

-Monil

itsme86 03-18-2005 01:10 PM

In my code, s is just a continually incrementing pointer to the next storage place in the string. The last thing it does is nul-terminate the string. So instead of using s after that point, try using str. It should contain the text that the user typed.


All times are GMT -5. The time now is 01:07 PM.