LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-17-2005, 11:57 AM   #1
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Rep: Reputation: 0
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
 
Old 03-17-2005, 12:02 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
What language?
 
Old 03-17-2005, 12:14 PM   #3
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
Sorry...... I need some sample code in C...

Thnx
 
Old 03-17-2005, 12:14 PM   #4
bullium
Member
 
Registered: Aug 2003
Location: Ohio
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146

Rep: Reputation: 17
writing your own keylogger?
 
Old 03-17-2005, 12:18 PM   #5
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
nah.. (I wish I could write one :-) )
I want to accept inputs like a paragraph or something and write it to disk blok...
 
Old 03-17-2005, 12:24 PM   #6
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Rep: Reputation: 45
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
 
Old 03-17-2005, 12:30 PM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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

Last edited by itsme86; 03-17-2005 at 12:31 PM.
 
Old 03-18-2005, 09:23 AM   #8
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 03-18-2005, 09:38 AM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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;
}
 
Old 03-18-2005, 11:49 AM   #10
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
Thnx.. BUt, I cant seem to print the contents of the string 's'... Am i missing something?

-Monil
 
Old 03-18-2005, 11:50 AM   #11
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Rep: Reputation: 45
You should open the file *fp and everythin should be in there. 's' is just a character that is being written to the file.
 
Old 03-18-2005, 11:53 AM   #12
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 03-18-2005, 12:10 PM   #13
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
$HTTP_POST_VARS doesn't accept disabled input fields xemous Programming 3 08-16-2005 05:49 AM
Can't get any output from my mic. Won't accept input. Please help Fear58 Linux - Hardware 1 07-06-2004 11:42 PM
RH 8.0 installer wont accept keyboard input exitsfunnel Linux - General 1 05-27-2003 10:18 AM
RH 8.0 installer won't accept keyboard input exitsfunnel Linux - Software 2 05-27-2003 06:20 AM


All times are GMT -5. The time now is 08:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration