0

Just a quick question, I'm reading from a file in C and just want to split the ID from the Name of the book, it'd be great if you could use simpler functions as this is for a assignment and don't want to use stuff I haven't learnt.

Layout of file:

1001 Harry Potter\n
1002 Lord of the Flies\n
1003 To Kill a Mockingbird\n

and so on, sorry if I didn't explain well enough

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • [Use `strtok_r` to _tokenize_ text](https://stackoverflow.com/questions/15961253/c-correct-usage-of-strtok-r) along defined boundaries (such as your `\n` characters). Avoid `strtok` because it's an ancient function that was designed in the days before reentrancy was considered important or useful. – Dai Jan 10 '22 at 04:38
  • @Dai: `strtok_r` is not part of ISO C, so OP may not be able to use it. – Andreas Wenzel Jan 10 '22 at 05:37
  • Have you learnt [`fgets`](https://en.cppreference.com/w/c/io/fgets), [`strtol`](https://en.cppreference.com/w/c/string/byte/strtol) and [`strchr`](https://en.cppreference.com/w/c/string/byte/strchr)? – Andreas Wenzel Jan 10 '22 at 05:39
  • 1
    Since `scanf()` is anything but simple, it presumably isn't an option for this exercise? It _can_ do the job, but the requisite format is not obvious. Is the ID to be treated as a number or a string? – Jonathan Leffler Jan 10 '22 at 05:41
  • @AndreasWenzel fgets is the only one out of those three, also have used scanf but after all my googling it doesn't look pretty haha – Xavier.code Jan 10 '22 at 05:53
  • @Xavier.code: What about [`strtok`](https://en.cppreference.com/w/c/string/byte/strtok)? Have you learnt that function? – Andreas Wenzel Jan 10 '22 at 05:55
  • @AndreasWenzel no but I just looked through and we can use some stuff we haven't been taught so I'd be happy to use it if its gonna be a lot easier – Xavier.code Jan 10 '22 at 06:09
  • @Xavier.code: You still have not answered the question by someone else whether the ID is supposed to be treated as a number or a string. Do you need to convert the ID to an `int`? Or do you only have to split the strings? – Andreas Wenzel Jan 10 '22 at 06:14
  • yes sorry, I want the ID to be an int and the name to be a string – Xavier.code Jan 10 '22 at 06:16
  • 1
    I suggest that you call `fgets` in a loop, in order to read one line per loop iteration. In every loop iteration, you can call `strtol` to convert the ID to a `long int`. If you pass the address of a pointer variable (have you learnt pointers yet?) as a second parameter to `strtol`, then the function will return a pointer to the first character that was not part of the number, which should be the space character. You can then assume that all remaining characters up to the newline character are part of the name. – Andreas Wenzel Jan 10 '22 at 06:21
  • yeah I have learnt pointers, as all the numbers will be 4 digits long, am I able to use that in the strtol function? – Xavier.code Jan 10 '22 at 06:30
  • @Xavier.code: The function `strtol` will not care how long the digits are, it will continue reading digits until it encounters the space character. – Andreas Wenzel Jan 10 '22 at 06:30
  • @Xavier.code: If you have trouble with your attempt, please [edit] your question and show the code of your attempt, and describe what problems you encountered. – Andreas Wenzel Jan 10 '22 at 06:49

0 Answers0