Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle Database Architecture

Authors: Thomas Kyte, Darl Kuhn

3rd Edition

1430262990, 9781430262992

More Books

Students also viewed these Databases questions

Question

3. Discuss the process of behavior modeling training.

Answered: 1 week ago