Question
C++ ONLY Write a function called getLinesFromFile that reads from a text file and stores its contents in an array. Each line in the file
C++ ONLY
Write a function called getLinesFromFile that reads from a text file and stores its contents 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 in the following order:
-
A string fileName
-
An array of integers int arr[]
-
The int length of the given array
-
-
Your function will open and read the file specified by the fileName parameter. This string should include both the name and extension of the file (what type of file it is). There are some good and bad examples below:
-
Good: books.txt
-
Bad: authors
-
-
Your function should read the file line by line, and at each line if there is a number it should store it in the array specified by the arr[] parameter.
-
If a line is empty it should continue reading until it sees another integer before adding to the array. Empty lines should not be added to the array.
-
Once the array is full (length integers have been added), subsequent integers in the file should be ignored.
-
If your function has added any integers to the array (meaning that the specified file exists and could be read), it should return the number of integers added to the array.
-
If the file does not exist, return -1.
-
Your function does not print anything
Example: if fileName.txt has the following contents:
1 2 78 77
The function call
getLinesFromFile("fileName.txt", wordArray, 4)
would return 4 and wordArray would look like
[1, 2, 78, 77]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started