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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-17-2005, 11:57 AM
|
#1
|
|
LQ Newbie
Registered: Mar 2005
Posts: 19
Rep:
|
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
|
|
|
|
03-17-2005, 12:02 PM
|
#2
|
|
Member
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349
Rep:
|
What language?
|
|
|
|
03-17-2005, 12:14 PM
|
#3
|
|
LQ Newbie
Registered: Mar 2005
Posts: 19
Original Poster
Rep:
|
Sorry...... I need some sample code in C...
Thnx
|
|
|
|
03-17-2005, 12:14 PM
|
#4
|
|
Member
Registered: Aug 2003
Location: Ohio
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 145
Rep:
|
writing your own keylogger? 
|
|
|
|
03-17-2005, 12:18 PM
|
#5
|
|
LQ Newbie
Registered: Mar 2005
Posts: 19
Original Poster
Rep:
|
nah.. (I wish I could write one :-) )
I want to accept inputs like a paragraph or something and write it to disk blok...
|
|
|
|
03-17-2005, 12:24 PM
|
#6
|
|
Senior Member
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044
Rep:
|
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 
|
|
|
|
03-17-2005, 12:30 PM
|
#7
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
|
03-18-2005, 09:23 AM
|
#8
|
|
LQ Newbie
Registered: Mar 2005
Posts: 19
Original Poster
Rep:
|
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
|
|
|
|
03-18-2005, 09:38 AM
|
#9
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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;
}
|
|
|
|
03-18-2005, 11:49 AM
|
#10
|
|
LQ Newbie
Registered: Mar 2005
Posts: 19
Original Poster
Rep:
|
Thnx.. BUt, I cant seem to print the contents of the string 's'... Am i missing something?
-Monil
|
|
|
|
03-18-2005, 11:50 AM
|
#11
|
|
Senior Member
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044
Rep:
|
You should open the file *fp and everythin should be in there. 's' is just a character that is being written to the file.
|
|
|
|
03-18-2005, 11:53 AM
|
#12
|
|
LQ Newbie
Registered: Mar 2005
Posts: 19
Original Poster
Rep:
|
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
|
|
|
|
03-18-2005, 12:10 PM
|
#13
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:45 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|