Question: Write a function getLinesFromFile that reads from a file of integers and stores its content in an array. Each line in the file will either

Write a function getLinesFromFile that reads from a file of integers and stores its content in an array. Each line in the file will either contain a single integer or be empty.

Your function should be named getLinesFromFile

Your function should take three parameters

A string filename

An array of integers

The length of the given array

Your function should fill the array with the integers in the file

Once the array is full (once length integers have been added) subsequent integers in the file should be ignored

Your function should return the number of integers added to the array if the file exists

If the file does not exist, return -1

Example:

if fileName.txt has the following contents:

1 5 23 18

The function call

int arr[4]; getLinesFromFile("fileName.txt", arr, 4);

would return 4 and arr would look like

[1, 5, 23, 18]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!