Reputation: 13
I'm trying to read the contents of a file object into an array of strings, but whatever I try nothing is displayed when I print the contents of the array. Specifically, I want to print the last ten lines of the text file by passing them into the array, and using a for loop on the array.
void FileReader::displayLast10records(){
ifstream ifile(filename);
string myArray[26];
cout << "\n" << filename << ": LAST 10 records in file \n\n";
for (int i = 0; i < numrecords; i++)
getline(ifile, myArray[i]);
ifile.close();
if (numrecords < 10)
{
for (int i = 0; i < numrecords; i++)
cout << setw(2) << (i + 1) << ".\t" << myArray[i] << endl;
}
else if (numrecords > 10)
{
for (int i = (numrecords - 10); i < numrecords; i++)
{
cout << setw(2) << (i + 1) << ".\t" << myArray[i] << endl;
}
}
}
The file(s) are simply large blocks of text, including spaces. The file looks like:
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely. The earliest programming languages predate the invention of the computer, and were used to direct the behavior of machines such as Jacquard looms and player pianos. Thousands of different programming languages have been created, mainly in the computer field, with many being created every year. Most programming languages describe computation in an imperative style, i.e., as a sequence of commands, although some languages, such as those that support functional programming or logic programming, use alternative forms of description.
I want to read each line into it's own element of a string array.
I do have another function that successfully uses getline() to display each line of the text file 10 lines at a time.
void FileReader::displayAllRecords(){
ifstream ifile(filename);
int displayed_lines = 0;
string arec;
cout <<"\n" << filename << ": ALL records in the file with line numbers, 10 at a time \n\n";
while (getline(ifile, arec))
{
if(displayed_lines % 10 == 0 && displayed_lines >= 1)
system("pause");
cout << setw(2) << (displayed_lines + 1) << ".\t" << arec << endl;
displayed_lines++;
}
ifile.close();
}
Upvotes: 1
Views: 10774
Reputation: 16940
Usually this is how you could read lines using ifstream:
#include <fstream>
#include <string>
#include <list>
using namespace std;
void readFile(const char* filename, list<string>& lines)
{
lines.clear();
ifstream file(filename);
string s;
while (getline(file, s))
lines.push_back(s);
}
or to read last 10 lines:
void readLast10(const char* filename, list<string>& lines)
{
lines.clear();
ifstream file(filename);
string s;
while (getline(file, s))
{
lines.push_back(s);
if (line.size() > 10)
lines.pop_front();
}
}
and then you can print last 10 lines:
int main()
{
list<string> lines;
readFile(filename, lines);
int n = 0;
printf("read %d lines\n", lines.size());
for (auto const it=lines.rbegin(); it!=lines.rend() && n<10; ++it, ++n)
printf("line%2u:\t%s\n", lines.size()-n, it->c_str());
}
or like that:
int main()
{
list<string> lines;
readLast10(filename, lines);
int n = 0;
cout << "read " << lines.size() << endl;
for (const auto& line : lines)
cout << l << endl;
}
Note that getline reads line by line, that is, if your text doesn't contain line breaks your entire file will be read as if it was only one file.
Upvotes: 1
Reputation: 14688
It's possible that your file doesn't contain proper line end markers like your program expects but your code assumes that file just got numrecords
end-of-line markers. Where that variable defined and how you get it is other question.
Do not use static array here, use std::list, use list::reverse_iterator
(can get value from list::rbegin()
, iterate 10 times unless iterator becomes equal to list::rend()) to iterate through last 10 records, your code would be far simpler (actually, just few lines)
Upvotes: 0