0

So basically the text file would look like this

Starting Cash: 1500
Turn Limit (-1 for no turn limit): 10
Number of Players Left To End Game: 1
Property Set Multiplier: 2
Number of Houses Before Hotels: 4
Must Build Houses Evenly: Yes
Put Money In Free Parking: No
Auction Properties: No
Salary Multiplier For Landing On Go: 1

All I need from the file is basically anything after ":" I'm just confused how to only read anything after a ":"? This is what I have right now. I just can't seem to think of a way to only scan for the numbers/yesorno.

void readRules(char*file_name)
{
  Rules r;
  FILE *file = NULL;
  file = fopen(file_name, "r");

  if (file == NULL) {
    printf("Could not open %s\n", file_name);
    return;
  }
  char c=fgetc(file);
  fscanf(file, "%c", &c);
  while (!feof(file))
  {
    fscanf(file, "%c", &c);
    if(c==':')
    {
      r.startCash=c;
    }
  }

  printf("There are %c word(s).\n", r.startCash);

  fclose(file);
}

Thank you.

andyong5
  • 149
  • 9
  • Have you considered `strtok()`? Why is it not suitable? – Yunnosch Jan 12 '18 at 08:20
  • Here is a question about [Read .CSV file in C](https://stackoverflow.com/questions/12911299/read-csv-file-in-c). You can probably adapt `;` to `:` as the separator. – Bo Persson Jan 12 '18 at 08:23
  • You file is organized in lines, so you should use `fgets` to get lines. Then you can use `strtok` to *tokienize* the file or simply `strchr` to find the position of the colon (`:`) – Serge Ballesta Jan 12 '18 at 08:35

1 Answers1

0

This program will read integers following a colon in each line of the file given. I imagine this is appropriate? You also have some strings after colons. If you want to read those, you can try scanning for a string "%s" and testing if the function returns nonzero (for at least one format pattern matched).

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

#define MAXLINE     1000

void readRules (const char *filename) {
    FILE *fp;
    char *lp, line[MAXLINE];
    int n;

    // Return early if file cannot be opened.
    if ((fp = fopen(filename, "r")) == NULL) {
        fprintf(stderr, "Error: Couldn't open \"%s\"!\n", filename);
        return;
    }

    // Use fgets to read consecutive lines. Returns NULL on error or EOF.
    while (fgets(line, MAXLINE, fp) != NULL) {

        // Read until newline is hit or buffer size exceeded.
        for (lp = line; *lp != '\n' && (lp - line) < MAXLINE; lp++) {

            // If encounter colon and sccanf reads at least 1 integer..
            if (*lp == ':' && sscanf(lp + 1, "%d", &n) == 1) {
                fprintf(stdout, "%d\n", n);
                break;
            }
        }
    }


    // Clean up.
    fclose(fp);
}

int main (int argc, const char *argv[]) {
    readRules("test.txt");
    return 0;
}

When run with your example input, it produces:

1500
10
1
2
4
1
Micrified
  • 3,338
  • 4
  • 33
  • 59
  • How would I separate each individual number into a separate int? Example: startCash=1500, turnLimit=10, numberOfProperties=1, etc.? – andyong5 Jan 15 '18 at 07:53
  • @AndyNguyen Where all of them are on the same line, and comma separated as you wrote? – Micrified Jan 15 '18 at 08:07