LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 01-08-2006, 02:11 AM   #1
sahel
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Rep: Reputation: 15
How to handle space in scanf?


How can i handle space in scanf?
ex i have this:
if((t=open(fdname,O_WRONLY)) == -1)
{
printf("error\n");
goto test1;
}

printf("Enter your recipiant:\n");
scanf("%s",&z);

write(t,z,strlen(z));
but when i want to read from this pipe , the read content is different from what i have written
thnaks
 
Old 01-08-2006, 02:39 AM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
First problem, your second argument to scanf should be just z instead of &z.
Second, don't use scanf() to read user input from stdin. Use fgets() instead.
fgets will properly check for buffer overflow and also read until enter key pressed and process spaces properly.
Code:
char z[256];
fgets(z,sizeof(z),stdin);
 
  


Reply



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
How to use the scanf( ) int C ? naihe2010 Programming 7 11-30-2005 02:35 AM
problem with scanf fssengg Programming 3 03-29-2005 04:27 AM
[SOLVED] how scanf can scan strings with space in betweeb vijeesh_ep Programming 4 02-26-2005 11:18 PM
scanf blackzone Programming 1 08-04-2004 01:50 AM
scanf help homerz Programming 2 11-11-2003 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration