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]);
Cœur
  • 37,241
  • 25
  • 195
  • 267
jsantos
  • 172
  • 1
  • 9
  • 1
    maybe use strtok and scan every item? – shaish Mar 28 '13 at 02:43
  • Your code looks fine. I am not sure but, the spaces after opening quote and before closing quote MAY be a Problem. – cipher Mar 28 '13 at 02:55
  • 1
    I think your question is interesting in itself. However, your code is wrong. You will keep on reading `Number` from the beginning of the line. So for example if you line is ` 1 2 3 4`, you will always read `1`. – joce Mar 28 '13 at 03: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. – jsantos Mar 28 '13 at 03:06
  • 1
    I agree with shaish opinion, strtok line into sections, then atoi each section – MYMNeo Mar 28 '13 at 03:09

3 Answers3

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);
}
MYMNeo
  • 818
  • 5
  • 9
  • 1
    Wow. That's an abuse of a `for` loop if I've ever seen one! :-D – joce Mar 28 '13 at 03:18
  • @Joce, Haha, it is a manual page of FreeBSD about `strtok`, when I first saw it, I had same confusion about the `for` loop – MYMNeo Mar 28 '13 at 03:22
  • I've tried it like this: for(i=0;i – jsantos Mar 28 '13 at 03:40
  • @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 http://man7.org/linux/man-pages/man3/strtok.3.html – MYMNeo Mar 28 '13 at 03:46
  • 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 :( – jsantos Mar 28 '13 at 03:50
  • it's not NULL, line2 is where I'm saving the content from the strtok() – jsantos Mar 28 '13 at 04:06
  • @jsantos You clearly haven't read about `strtok`. Please do before doing anything else: http://msdn.microsoft.com/en-us/library/2c8d19sb(v=vs.100).aspx – joce Mar 28 '13 at 04:29
  • Joce's suggestion is good.You can not change the cutting string while invoking `strtok`. – MYMNeo Mar 28 '13 at 04:34
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
MYMNeo
  • 818
  • 5
  • 9
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++;
}
jsantos
  • 172
  • 1
  • 9
  • Honestly, you should reward @MYMNeo by giving him the "approved checkmark". He did most of the job for you. – joce Mar 28 '13 at 04:40
  • 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 – jsantos Mar 28 '13 at 04:44