Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[beginner] C reading lines, and numbers

Name: Anonymous 2009-08-29 8:56

I have a file in which ever line of text is terminated with a number. I'd like to extract the text and the number to variables, how do I do that?

Name: Anonymous 2009-08-29 13:29


int doit() {
  char buf[64];
  int n = 0;
  size_t i, x = 1;

  if(fgets(buf, 64, stdin)) {
    i = strlen(buf);
    while(i && isdigit((unsigned char)buf[--i]))
      n += (buf[i] - '0') * x, x *= 10;
    if(buf[i] == '-') return -n;
    return n;
  }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List