could someone fix the error for me? int getLinesFromFile(string filename,string
ID: 3725037 • Letter: C
Question
could someone fix the error for me?
int getLinesFromFile(string filename,string arr[],int size)
{
ifstream in(filename.c_str());
if(!in.is_open())
{
return -1;
}
int count =0;
string line;
while(getline(in,line))
{
if(count<size)
{
arr[count++]=line;
}
}
in.close();
return count;
}
Write a function getLinesFromFile that reads from a file and stores its content in an array The function takes three parameters: a string fileName, a string array wordArray, an integer sizeArray getLinesFromFile should open the file in read mode, read each line, store each line in the array The function should return the number of lines placed into the array. If any empty lines are encountered, those should not count towards the count of the number of lines and should not be added to the array If the file does not exist, return-1. · Example: if fileName.txt has the following contents: sky night 78 ski season The function call getLinesFromFile(fileName.txt, wordArray, 4) would return 4 and wordArray would look like 'sky". "night", "78", "ski season"]Explanation / Answer
int getLinesFromFile(string filename,string arr[],int size)
{
ifstream in(filename.c_str());
if(!in.is_open())
{
return -1;
}
int count =0;
string line;
while(getline(in,line))
{
if(line.size() != 0) {
if(count<size)
{
arr[count++]=line;
}
}
}
in.close();
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.