Better than scanf


 
Thread Tools Search this Thread
Top Forums Programming Better than scanf
# 1  
Old 03-24-2010
Better than scanf

I don't know how to do this:

Code:
printf("creazione nuovo messaggio\n");
             printf("insert dest\n");
             scanf("%s",dest);
             printf("insert object\n");
             scanf("%s",ogg);
             printf("inserire text\n");
             scanf("%s",test);
             printf("%s\n",dest);
             printf("%s\n",ogg);
             printf("%s\n",test);
             strcpy(messaggio,dest);
             strcat(messaggio,"/");
             strcat(messaggio,ogg);
             strcat(messaggio,"/");
             strcat(messaggio,test);
             strcat(messaggio,"/");
             printf("%s",messaggio);

For example it do this:

dest=jonny
ogg=hello
test=how are you

and it prints

jonny
hello
how

why doesn't it print all that I write but only the first wordSmilie
# 2  
Old 03-24-2010
Quote:
Originally Posted by italian_boy
why doesn't it print all that I write but only the first wordSmilie
Because that's the behaviour of that function - according to the man page, %s matches "a sequence of non-white-space characters". If I remember correctly, you should see the terminating new line if it's there in the input. So, all you have to do to read a sequence of characters that includes whitespace is put scanf() in a while() loop that exits once it's spotted a newline. Although you *may* have to use getchar() to skip past whitespace... I'm not too familiar with this family of functions.

Either way, if you play around a bit it shouldn't be too hard.

---------- Post updated at 07:29 AM ---------- Previous update was at 07:23 AM ----------

Oh yeah, I should have mentioned - a perhaps-easier alternative would be to use a regex-style set, e.g:

Code:
scanf("%[a-zA-Z0-9 ]", test);

to allow all numbers, letters and a space.

(NOT TESTED)
# 3  
Old 03-24-2010
Thank you very much!
Here is the exactly code to read whitespaces too:

Code:
             puts("insert text ");
             while(getchar()!=(int)'\n');
             scanf("%[^\t\n]",test);
             puts(test);

# 4  
Old 03-25-2010
You could also use fgets, which always retrieves an entire line:

Code:
char buf[512];
printf("Input something: ");
fgets(buf, 512, stdin);

It gives you the entire string including the return character though! Smilie So:

Code:
char buf[512];

printf("Input something: ");
if(fgets(buf, 512, stdin) != NULL)
{
        size_t pos=strlen(buf);
        if(pos > 0)
        {
                if(buf[pos-1]) == '\n') buf[pos-1]='\0';
        }
}

This will input an entire line and chop the newline off the end.
# 5  
Old 03-25-2010
Thank you Corona! But it's a little bit difficoult then the other!I'll try it but I think I'll use the other... Smilie
# 6  
Old 03-26-2010
scanf may be easier(or at least more obvious) but will cause strange problems -- as you've already seen. And there's others too. Try this:

Code:
#include <stdio.h>

int main(void)
{
        int fail=1, n;
        while(fail)
        {
                fail=0;

                printf("\nPlease enter a number: ");
                if(scanf("%d", &n) != 1)
                        fail=1;
        }

        printf("You input %d\n", n);
        return(0);
}

When you enter a number, it works fine. When you enter qwertyuiop, it goes into an infinite loop. This is because scanf only reads one single character, finds that it's not a number, gives up, and puts it back so the data you get the next loop is the same you had last time. You have to fflush(stdin); to get rid of the backed-up garbage.

If you're inputting whole lines, scanf is the wrong function to use.

Often I use fgets and scanf together to avoid that buffer-problem, like this:

Code:
char buf[512];
int n;
// Read in an entire line
fgets(buf,512,stdin);
// scan the string for the data you want
sscanf(buf, "%d", &n);

Note the extra s, that's intentional. sscanf works just like scanf except it scans strings. This way, if scanf has an error, the data won't remain stuck in the buffer until flushed since it's already been read.

Last edited by Corona688; 03-26-2010 at 12:40 PM..
# 7  
Old 03-27-2010
Mmm very good idea!! I'll try it ;-)!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Scanf() string pointer problem

I have a problem with scanf() for string pointer as member of a struct. #include <stdio.h> #include <stdlib.h> struct Student { int studentNumber; int phoneNumber; char *studentName; //line 7 // char studentName; //line 8 }; int... (10 Replies)
Discussion started by: yifangt
10 Replies

2. Shell Programming and Scripting

Passing argument 1 of 'scanf' makes po

$ cc Array.c Array.c: In function ‘main’: Array.c:23: warning: passing argument 1 of ‘scanf’ makes po Array.c:25: error: expected expression before ‘return’ Array.c:29: error: expected expression before ‘return’ Array.c: At top level: Array.c:44: error: expected ‘)’ before ‘&’ token... (8 Replies)
Discussion started by: sgradywhite
8 Replies

3. Programming

What is the difference between printf and putchar() or scanf and getchar() ?

Im a newbie to programming language, i found tat there r these function called printf and putchar() as well as scanf and getchar(), im curious abt why do dey hav these 2 different function although dey r doing the same instruction? :confused: (13 Replies)
Discussion started by: kris26
13 Replies

4. Programming

simple scanf issue ?

Hello everyone, I hope someone is awake to help me on this.. hey How can I do something like this: The user is asked is asked to enter an int value, but I want to provide a default value on stdout, which they can back space and change it to whatever they want.. for e.g: Enter the... (4 Replies)
Discussion started by: the_learner
4 Replies

5. Programming

problem with scanf

hi all! i've written a simple c program: #include<stdio.h> #include<stdlib.h> int main() { int a; char b; char c; ... (4 Replies)
Discussion started by: mridula
4 Replies

6. Programming

scanf doesn´t reads spaces ???

hi all i have a program in C (Unix Solaris 5.7) and i want to read a string from keyboard, but the "scanf" doesn´t reads spaces. example: .... char name; .... printf("Enter your name: "); scanf("%s",&name); printf ("Your name is: %s", name); and if i write Kevin Costner ... (4 Replies)
Discussion started by: DebianJ
4 Replies

7. Programming

scanf with strings... please help

hi i am a beginner to C i have encountered a problem with my assignment, and i have researched it on the internet, but unfortunately i didn't find anything related to that. i am writing a simple program that takes user's input by prompt command, and parse the whole line into an array of... (1 Reply)
Discussion started by: inkfish
1 Replies

8. Programming

Scanf problem under LINUX...

I have a problem reading characters from keyboard with the scanf function. Here there is a little piece of code: #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> /* The last 3 libraries are included because in the real program I use some... (4 Replies)
Discussion started by: robotronic
4 Replies
Login or Register to Ask a Question