0

Hey guys! I'm new to C, so i would like help in one problem. I have to separate a string and put him into diferent variables. Imagine, Sol 3 5 would result in something like:

var1=Sol  
var2=3  
var3=5 

I tried to use the scanf, but it stoped in the first space :/ .
Thanks in advance!
Cheers!

EDIT: Isn't my homework, i'm just practicing, but i really want to now how I can do this :) . The code I have now is this:

int main () {  
    char var1[10],var2[10],var3[10],func;  
    fgets(func, 20, stdin);   
    fscanf(func,"%s %d %d", var1,var2,var3);  
    printf("%s %d %d", var1,var2,var3);  
    return 0;  
}
4
  • 3
    please post the code you have so far. Commented Apr 2, 2011 at 17:06
  • 3
    Is this homework? No problem if it is ... just let us know and we'll answer with that in mind. Commented Apr 2, 2011 at 17:09
  • Isn't my homework, just practicing :) . I put the code I have now on the topic, to give an ideia of what i'm trying to do. Commented Apr 2, 2011 at 17:32
  • 1
    This code doesn't look right. Does it even compile? func needs to be an array, scanf needs to receive pointers (&), var3 and var3 shoudl have been integers and fscanf receives a FILE*, not a string. Commented Apr 2, 2011 at 17:44

3 Answers 3

4

After the edit and code posted

Your problem is that you are lying to the compiler. Don't do that. It doesn't like it :)

You ask the compiler to read a string and 2 integers ... but then tell it to put the results in a char arrays (correct only for the first conversion)

             /* char[] but %d wants pointer to int */
    fscanf(func,"%s %d %d", var1,var2,var3);
                         /* all var1, var2, and var3 are arrays of char! */

Try declaring your variables as

    char var1[10];
    int var2,var3;

Oh! func is declared as plain char. You probably want something else.

Once you declare your variables like that, you need to change the scanf call an pass the address of the ints, rather than their (uninitialized) values.

Sign up to request clarification or add additional context in comments.

1 Comment

One more important thing: you should always test the return value of most Standard C functions; in particular the scanf family (also the malloc family). So ... write if (scanf(...) != 3) { /* oops */ } rather than scanf(...);
4

strtok is the function you need:

 #include <string.h>

 char* str = "Sol 3 5";
 char* ptr;
 char* saved;
 ptr = strtok_r(str, " ", &saved);
 while (ptr != NULL)
 {
   printf("%s\n", ptr);
   p = strtok_r(NULL, " ", &saved);
 }

Just a note: this function modifies the original string, placing end of string tokens (nulls, \0) in place of delimiters.

Ok scanf would be good anyway, but I'm not gonna help you if you don't state clearly if it's homework..

7 Comments

New code should not use strtok. Use strtok_r instead, which does much the same thing but is thread-safe.
Yes, sorry, forgot about reentrancy :)
@Jack: srttok and strtok_r have different argument lists; @John: strtok_r is POSIX, so not universally (in the C compilers universe) available.
Isn't my homework, i'm practicing, but i really want to now how do this. In the code you type, the function don't save the string into variable, just print the string right?
yes, it just prints it but the pointer can be used for your purposes, you can actually dupe it with strdup and forget about the fact.
|
0

There is a function similar to scanf called sscanf. It works just the same, but it reads to values from a string instead of from a file:

sscanf("Sol 3 5", "%s%d%d", v1, &v2, &v3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.