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 11-11-2003, 04:41 AM   #1
homerz
LQ Newbie
 
Registered: Oct 2003
Posts: 9

Rep: Reputation: 0
scanf help


I want to make a C program in emacs. The program should read several strings from the user at one time and do stuff with the strings. Between each string and the other there exist a space. What function should i use and how can i use it so when the scanff reaches a space it will know that the first string has ended and start with the second string. Thanks.
 
Old 11-11-2003, 05:24 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
#include <stdio.h>

int main(){
 char word1[80],word2[80];
 memset(word1,0x00,sizeof(word1));
 memset(word2,0x00,sizeof(word2));
 printf("enter two words: ");
 scanf("%s %s",word1,word2);
 printf("word1=%s word2=%s \n",word1,word2);

 return 0;
}

This reads two "words" separated by spaces from stdin (the keyboard in this case).

scanf is NOT a safe function because it does not check the size of the data item being input. scanf usage in older programs is a source of vulnerability.
 
Old 11-11-2003, 05:48 AM   #3
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
Now that we're talking vulnerabilities...

Never EVER write this kind of code:

printf(word1);


always do it like this: (like jim mcnamara did)

printf("%s", word1);


The first style is vulnerable to a user (or a hacker) entering strings with '%' characters.

Martin
 
  


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
scanf question exvor Programming 5 07-24-2005 04:36 PM
problem with scanf fssengg Programming 3 03-29-2005 04:27 AM
scanf question jnusa Programming 4 01-07-2005 09:04 AM
scanf blackzone Programming 1 08-04-2004 01:50 AM
Is it safe to use scanf for anything?? purpleburple Programming 5 07-03-2002 06:19 PM

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

All times are GMT -5. The time now is 02:55 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