0

I am trying to read a file that is filled with ints and strings. The file starts with 1 string followed by 2 ints (this is always the case). Then the rest of the data in the file is strings (this can range from 1 string to hundreds). I want to store the first string into a variable and the two ints into their own separate variable and the strings into an array.

I initially tried using fscanf but sense the file can contain an arbitrary number of strings after the 2 ints. I figured this would not work. I am now thinking that I need to somehow parse the data / tokenizing the data. Any ideas would be appreciated.

DATA FILE

S11FREE
2
0
(A,B)(C,D)
(E,F)(G,H)
(I,J)(K,L)
(M,N)(O,P)
...
...
...
...

(...) as in more strings in the same format

Matthew
  • 5
  • 5
  • Hint - you read the first string and the two ints. Then you set up a loop. Keep checking the return value of `fscanf`. If it is equal to the no of elements that you read, then continue with the loop, else exit the loop. – Rishikesh Raje Sep 17 '19 at 04:36
  • `fgets` could be your friend.... perhaps `sscanf` could be handy as well – Support Ukraine Sep 17 '19 at 05:13
  • @RishikeshRaje While you are correct with what you say, I believe that OPs main problem is to create an array which can take all the input before knowing how much it is. – Yunnosch Sep 17 '19 at 05:14
  • For reading the file: https://stackoverflow.com/a/9206332/4386427 – Support Ukraine Sep 17 '19 at 05:15
  • Matthew, if you really need to read all of the input into one single simple (as opposed to e.g. linked lists...) data structure (it is not going to be a standard-conforming array), then you probably have to read the file twice. Once for determining the size and then for reading it in. Instead of making an array (always static, size known before compiling; as long as you do not employ non-standard variable lenght arrays) you could probably use `malloc()`. Consider however whether you can guarantee that your memory suffices. – Yunnosch Sep 17 '19 at 05:16
  • Thanks for the input everyone I will give them a try, For the array sizing issue. Like said above I was either going to use malloc and if the array size was to small just realloc until it is big enough. Or simply go through the file twice and subtracting 3 to get the correct amount of strings. – Matthew Sep 17 '19 at 05:22
  • If you have already mentioned malloc, realloc or reading twice in your question, then you have posted a different question than you are referring to. The question you have posted mentions only an array for the repeated part of the input. If you already know about those concepts, then what is actually your question? – Yunnosch Sep 17 '19 at 05:30
  • You are correct. Initially I planned for an array. Now after some insight from comments I am leaning towards using malloc. I will edit the post accordingly. Thanks – Matthew Sep 17 '19 at 05:37

1 Answers1

0

You can follow these steps:

  • read in the string, any way you are comfortable with
  • read in the integers, dito
  • read in the rest of the file, but ...

either

  • ignore it, apart from the size of the strings, which you accumulate
  • use the knowledge of the size to malloc() a suitably sized memory part (hopefully not beyond your total available memory
  • read in the file again and store the strings into consecutive parts of the reserved memory
  • keep track of which string you store where (e.g. by storing the locations separately) or insert \0 between them (do not forget to calculate them into the total size)

or

  • read the input into a growing data structure like e.g. a linked list
  • process it from there

or

  • temporarily store into a linked list
  • when done you know the total size
  • then create a memory with malloc() and move the data there, while deleting the linked list

or the idea you mention in a later comment, using realloc.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54