Day 3 1-3 Thursday 2019-01-17

2019-01-18  本文已影响0人  顾清_

data.txt

Pepsi: 80, okay
Ginger Ale: 97, great
Mt Due: 20, no
Coke: 80, okay
Moxie: 100, yes
DrPep: 75, kinda

parse.c

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

#define MAXLINESIZE 255
#define MAXTOKENSIZE 100

int main (int argc, char *argv[]) {
  char inString[MAXLINESIZE];
  char soda[MAXTOKENSIZE];
  char score[MAXTOKENSIZE];
  char comment[MAXTOKENSIZE];

  char *token;

  FILE *fp = fopen(argv[1], "r");
  if (fp == NULL) {
    printf("Bad filename\n");
    return 1;
  }
  while(fgets(inString, MAXLINESIZE, fp) != NULL) {
    token = strtok(inString, ":,\n");
    strcpy(soda, token);
    token = strtok(NULL, ":,\n");
    strcpy(score, token);
    token = strtok(NULL, ":,\n");
    strcpy(comment, token);
    printf("Soda: %s, Score: %s, Comment: %s\n", soda, score, comment);
  }
  return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLINESIZE 255
#define MAXTOKENSIZE 100

int main (int argc, char *argv[]) {
  char inString[MAXLINESIZE];
  char soda[MAXTOKENSIZE];
  char score[MAXTOKENSIZE];
  char comment[MAXTOKENSIZE];

  FILE *fp = fopen(argv[1], "r");
  if (fp == NULL) {
    printf("Bad filename\n");
    return 1;
  }
  while(fgets(inString, MAXLINESIZE, fp) != NULL) {
    strcpy(soda, strtok(inString, ":,\n"));
    strcpy(score, strtok(NULL, ":,\n"));
    strcpy(comment, strtok(NULL, ":,\n"));
    printf("Soda: %s, Score: %s, Comment: %s\n", soda, score, comment);
  }
  return 0;
}

H e l l o
H in String token -> l l o

instring = Pepsi:85,okay/n
strtok(inString, ":");
inString token -> pepsi - 85, okay
strtok(inString, ",");
inString token -> Pepsi - 85, okay
token - 85
strtok(inString, "/n");
pepsi -> 85 -> okay
token - okay

strcpy(current->data, strtok(inString, ":/n"));
strcpy(current->value, atoi(strtok(NULL, ":/n")));
上一篇下一篇

猜你喜欢

热点阅读