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

#define BUF_SIZE 256


int main() {
  char* file_name = "tmp/uhoh.txt";
  char buf[BUF_SIZE];
  int x_pos;
  int y_pos;
  int ret;
  
  FILE *file = fopen (file_name, "r");
  if (file==NULL) {
    printf ("Failed opening file: %s\n", file_name);
    return 1;
  }

  while (fgets(buf, BUF_SIZE, file)) {
    ret = sscanf(buf, "%dx%d", &x_pos, &y_pos);
    if (ret!=2) {
      if (buf[strlen(buf)-1]=='\n') {
        buf[strlen(buf)-1]='\0';
      }
      fprintf(stderr, "Failed parsing string '%s'\n", buf);
    } else {
      fprintf(stdout, "x_pos: %d   y_pos: %d\n", x_pos, y_pos);
    }
  }

  ret = fclose(file);
  if (ret!=0) {
    fprintf(stderr, "Failed closing file '%s'\n", file_name);
  }
  return 0;
}







int main2() {
  char* file_name = "tmp/uhoh.txt";
  int ret;
  char buf[BUF_SIZE];
  int x_pos;
  int y_pos;
  
  FILE *file = fopen (file_name, "r");
  if (file==NULL) {
    printf ("Failed opening file: %s\n", file_name);
    return 1;
  }

  while (fgets(buf, BUF_SIZE, file)) {
    /* printf ("line: %s", buf); */
    ret = sscanf(buf, "%dx%d", &x_pos, &y_pos);
    if (ret==2) {
      printf ("x_pos: %d   y_pos: %d\n", x_pos, y_pos);
      /*} else {
        if (buf[strlen(buf)-1]='\n') buf[strlen(buf)-1]='\0';
        printf ("Failed parsing data from '%s': '%s'\n", file_name, buf);
        }*/
    }
  }
    //fclose(file);
  return 0;
}
