LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-02-2004, 03:26 AM   #1
chiahsin
LQ Newbie
 
Registered: Dec 2004
Posts: 6

Rep: Reputation: 0
Talking scanf strange behavior


hello:

Here is the program I try to send queue,I try to read some imformaiton from stdin and load it in structure and then send to queue, but the scanf don't wait for me to input value
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
extern int errno;
int main()
{
struct msgbuf1{
long mtype;
char mtext[100];
} sndbuf , *msgp;
int rtrn, msgqid ;
char strbuf[100];
if ( (msgqid = msgget(0x888, IPC_CREAT|0600)) == -1 )
{fprintf(stderr, "msgget 888 failed !\n");exit(1);}
while(1)
{
msgp=&sndbuf;
printf("請輸入交易種類\n");
scanf(strbuf);
msgp->mtype=atoi(strbuf);
printf("請輸入帳戶姓名\n");
scanf("%s",sndbuf.mtext);
strcat(sndbuf.mtext,"|");
printf("請輸入帳戶餘額\n");
scanf(strbuf);
strcat(sndbuf.mtext,strbuf);
rtrn=msgsnd(msgqid,msgp,strlen(sndbuf.mtext),0);
if (rtrn == -1)
{perror("msgsnd");exit(2);}
}
}
 
Old 12-02-2004, 05:51 AM   #2
shmonkey
Member
 
Registered: Nov 2004
Location: UK
Distribution: Ubuntu
Posts: 118

Rep: Reputation: 15
I'm not sure scanf(strbuf); is allowed, does it work ?

try using fflush(stdin); after each call to scanf.

Note: fgets is considered a better (more secure) function to use then scanf.

Regards

Shmonkey
 
Old 12-02-2004, 06:25 PM   #3
chiahsin
LQ Newbie
 
Registered: Dec 2004
Posts: 6

Original Poster
Rep: Reputation: 0
hello:

it's the scanf usage problem, thank you !!!
 
  


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
Very Strange Behavior raysr Mandriva 4 08-31-2004 02:06 PM
Strange Iriver behavior DizzyG Linux - Hardware 2 06-14-2004 04:43 PM
Strange Behavior andrewb758 Linux - Hardware 5 08-31-2003 02:42 PM
strange behavior abhijit Linux - General 3 07-09-2003 11:25 PM
Strange DHCPcd behavior padukes Linux - Networking 0 03-17-2002 09:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:02 PM.

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