2

Here is my input:

 david 10 40 70
 sam 9 45 31
 miranda 10 20 50
 zhang 10 26 41

I am trying to add all these numbers for each line and then print them out in terminal like this:

david 120
sam 85
etc...

how do I sum these numbers starting from the second word in a line? Here's my code for some context:

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

 FILE *fr;
 char * line = NULL;
 size_t len =0;
 ssize_t read;

 int main(int argc, char* argv[]){

    fr = fopen(argv[1], "r");
        if(fr==NULL){
            exit(EXIT_FAILURE);
        }

         while ((read = getline(&line, &len, fr)) != -1){
            printf("%s", line );
        }
        fclose(fr);
        if (line){free(line);}
    return 0;
 }
Barney Chambers
  • 2,720
  • 6
  • 42
  • 78
  • Are there always 3 numbers after the name? – 1.618 May 11 '15 at 03:54
  • There can be any amount of numbers – Barney Chambers May 11 '15 at 04:01
  • oh -- I was going to suggest `sscanf()`, but if the number of numbers is inconsistent, it isn't going to work. – 1.618 May 11 '15 at 04:02
  • 1
    You probably want to review [How to use `sscanf()` in loops?](http://stackoverflow.com/questions/3975236) — or, at least, it gives you one way of going about the business of analyzing the numbers on the lines you read. Good job on using `getline()`. – Jonathan Leffler May 11 '15 at 04:52

2 Answers2

3

You could try strsplit() (EDIT: Or strtok() as @1.618 suggests, a bit different that PHP's strsplit) for each line, and then use atoi().

I'm not sure that strsplit() exists in C standard libraries, you may have to recode it yourself : it takes a char * (your string to split) and a char (the delimiter, in this case a space), and should return a char ** in which are your substrings (the words) you could pass to atoi().

vmonteco
  • 14,136
  • 15
  • 55
  • 86
  • strsplit() is a php function, not c – 1.618 May 11 '15 at 04:05
  • 2
    the equivalent c function is strtok() – 1.618 May 11 '15 at 04:06
  • I know, but that is not forbidden to recode it in C. :) (I had to do it myself in my school as an exercise). There may be a better way to do, but at least I can suggest this one. And I'm pretty sure you can find some sources on the net if you don't want to recode it yourself. EDIT: I didn't know strtok(). hasn't it to be called several times? But it could be usefull though. :) – vmonteco May 11 '15 at 04:08
  • 1
    http://stackoverflow.com/questions/4513316/split-string-in-c-every-white-space please check this link also.. – Nirav Kamani May 11 '15 at 04:16
  • thanks for the suggestion. how do I remove the first character from the equation so that it is not incorrectly added to the sum of numbers? – Barney Chambers May 11 '15 at 06:20
  • If you use **strsplit()**, you just ignore the first **char \*** of the resulting **char \*\***. If you use **strtok()**, just call it once before adding numbers to skip it (you get each number by calling it successively to get each "word" if I understand the manual well). So to sum up you just don't use the first word in your calculation. – vmonteco May 11 '15 at 06:26
1

If your buffer is null terminated, you can do it in place, without special string splitting functions, and handle an arbitrary number of numbers.

int sumline(char *buf)
{
  int sum=0;
  size_t i;
  for(i=0; buf[i] != '\0'; i++)
  {
    if(buf[i] == ' ' && isdigit(buf[i+1]))
    {
      sum += atoi(buf+i+1);
    }
  }
  return sum;
}

Just iterate over the characters, and whenever you hit a space, run atoi on the string starting from the next character.

Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
  • 1
    If the input had two spaces between a number, your code would count that twice, would it not? It would be safer to use `strtol()` because it tells you the first character it didn't process. – Jonathan Leffler May 11 '15 at 04:54
  • shrug, question didn't say anything about arbitrary space. but, that's easily dealt with. see edit. – Jay Kominek May 11 '15 at 14:06