2

I'm trying to scanf multiple ints from a string, but I don't know how many it will have, because it varies from case to case. I want to scanf multiple numbers and put them in an array.

I've been trying to do it this way but it's not working... Assuming I want to scan C numbers from the string "line".

for(a=0;a<c;a++)
    sscanf(line, " %d ",&v[a]);
5
  • 1
    maybe use strtok and scan every item? Commented Mar 28, 2013 at 2:43
  • Your code looks fine. I am not sure but, the spaces after opening quote and before closing quote MAY be a Problem. Commented Mar 28, 2013 at 2:55
  • 1
    I think your question is interesting in itself. However, your code is wrong. You will keep on reading <space>Number<space> from the beginning of the line. So for example if you line is ` 1 2 3 4, you will always read 1`. Commented Mar 28, 2013 at 3:00
  • 1
    @cipher I've already tried it with and without the spaces... It didn't work either way. But I understand what you're saying. Commented Mar 28, 2013 at 3:06
  • 1
    I agree with shaish opinion, strtok line into sections, then atoi each section Commented Mar 28, 2013 at 3:09

3 Answers 3

1

I wrote a piece of code to help you understand it more clearly.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    int v[10];
    char buffer[4096];

    char * line = NULL;
    int i, j;


    if(fgets(buffer, 4096, stdin) == NULL){
        return -1; 
    }   

    for(i = 0, line = strtok(buffer, " "); i < 10; i++){
        if(line == NULL){
            break;
        }   

        sscanf(line, "%d", &v[i]);
        line = strtok(NULL, " "); 
    }   
    j = i;

    for(i = 0; i < j; i++){
        printf("%d\n", v[i]);
    }   

    return 0;
}

[neo]:./a.out 
1 2 3 4 5 9999
1
2
3
4
5
9999
Sign up to request clarification or add additional context in comments.

2 Comments

I much prefer your "horrendous abuse of a for loop" answer!!
@Joce, it is "common" when in some source code.:) isis.poly.edu/kulesh/stuff/src/klist/list.h
1

Suppose you have enough space to store as much as integer.

char * c_num = NULL;
for(c_num = strtok(line, " \t\n"), a = 0; c_num != NULL && a < c; c_num = strtok(NULL, " \t\n"), a++){
    v[a] = atoi(c_num);
}

8 Comments

Wow. That's an abuse of a for loop if I've ever seen one! :-D
@Joce, Haha, it is a manual page of FreeBSD about strtok, when I first saw it, I had same confusion about the for loop
I've tried it like this: for(i=0;i<c;i++){ line2 = strtok(NULL, " "); sscanf(line2,"%d",&v[i]); } but it's giving me a segmentation faul error... Any thoughts?
@jsantos, I think you have misuse strtok, while cutting string, you have to invoke strtok(@your cutting string, @separator) first, then loop invoke strtok(NULL, @separator) to get remaining cut strings.So you have miss the first step.Here is manual man7.org/linux/man-pages/man3/strtok.3.html
My bad, I had it written on my code just like you said, I just didn't copy it into here... So the problem maintains :(
|
0

I finally nailed it! Thanks for all your help guys ;)

f = fopen(argv[1],"r");
fgets(line,c,f);

line2 = strtok(line, " ");

while (line2 != NULL){

    sscanf(line2, "%d",&v[i]);

    line2=strtok(NULL, " ");
    i++;
}

2 Comments

Honestly, you should reward @MYMNeo by giving him the "approved checkmark". He did most of the job for you.
I'm sorry, I just registered a couple of days ago, I wasn't familliar with the way the website works, but it's done! :D

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.