Home > Back-end >  Read File that you don't know the lenght of
Read File that you don't know the lenght of

Time:12-02

I have coded a program that reads an integers & tells if it's greater or not. I have done it with an array & initialized it with some numbers(As a test if the program works). Now I have to read it from a File with a lot of integers, but I don't know the length of the File. How can I determinate the size of the Array if I don't know the length?

CodePudding user response:

Either you do it on the fly (read and process one line at a time -> one number at a time), or you need to allocate memory dynamically as you read the file as described here: Dynamically expanding array using realloc

CodePudding user response:

Do you need to use an array? I think using list is your best option here. Than you can just use something like myList.add(newElement) or myList.append(newElement) depending on the language you are using.

  •  Tags:  
  • c c99
  • Related