0

I need to be able to get data from a text file, which is presented in the following format:

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

To clarify, I need the data presented after the colon. I'm not really sure how to approach this. I was reading other questions and they all said to use fgets, but I don't know how long each line will be and we can't statically allocate C strings, so where would I store the line pointed to by fgets? Also, is it possible to do this using fscanf (we have learned how to do fscanf but not learned fgets)? My idea when approaching this was to get each line, and then scan each line with sscanf (I think that would work) using the string literals that I don't need:

sscanf(str, "Starting Cash: %d", &startingCash);

Would this work?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
Ismael
  • 197
  • 1
  • 2
  • 12
  • You can use `strchr` to locate the position of the colon in the line. Or you can use `strtok` to parse the line – Pablo Jan 24 '18 at 22:43
  • 1
    *"we can't statically allocate C strings"*. The simple way is to define a local array big enough to hold the longest input you expect. Use `fgets` and restrict the input length, as it requires. – Weather Vane Jan 24 '18 at 22:49
  • 1
    *"I need the data presented after the colon."* Is there any guarantee that the data is presented in the same sequence? – Weather Vane Jan 24 '18 at 22:51
  • If not you will need to use `strstr()` to find a decisive substring inside your line or a custom regexp matcher and then use `atoi()` for numbers or `strcmp()` for "Yes" and "No" statements according to what you think it is. – ft_error Jan 24 '18 at 22:54
  • Take the [tour], read [Ask] and [MCVE]. You obviously haven't worked this problem hard enough to have sufficient code to have run into a real problem yet. I'd be willing to bet you can find other SO users who have worked exactly the same assignment. Some of them probably got answers. Lookup `malloc()` and `free()` or simply create an array on the stack for your string buffer. Yes, `sscanf()` is a good choice for finding prefixes in strings and converting tokens to whatever type you need. With any luck, nobody here will post a solution to your homework assignment. – jwdonahue Jan 25 '18 at 00:21
  • 2
    Or simply use *character oriented input* like `fgetc` and check for `':'` and then `'\n'` (the stuff in between, minus any leading or trailing whitespace, is what you want) – David C. Rankin Jan 25 '18 at 05:02
  • 1
    @ft_error [`atoi` is harmful and shouldn't be used](https://stackoverflow.com/q/17710018/995714) – phuclv Jan 25 '18 at 14:19

1 Answers1

1

After opening the file with fopen(), you could do

float cash;
int turnlimit, plyrno, mult, house;
fscanf(fin, "%*[^:]: %f", &cash);
fscanf(fin, "%*[^:]: %d", &turnlimit);
fscanf(fin, "%*[^:]: %d", &plyrno);

where fin is the FILE pointer.

%[^:] would scan till, but not including, a : is encountered and the * is for assignment suppression; meaning the value for it would be read but not assigned anywhere.

After reading till the point before the :, the : itself followed by a space must be read. So a : must be there in the format string followed by a space.

See What is the purpose of using the [^ notation in scanf? .

J...S
  • 5,079
  • 1
  • 20
  • 35