【发布时间】:2017-09-12 02:34:18
【问题描述】:
我正在尝试将文件对象的内容读入字符串数组,但无论我尝试什么,当我打印数组的内容时都不会显示任何内容。具体来说,我想通过将文本文件的最后十行传递到数组中并在数组上使用 for 循环来打印它们。
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;
}
}
}
文件只是大块的文本,包括空格。该文件如下所示:
编程语言是一种人工设计的语言 向机器传达指令,特别是 一台电脑。编程语言可用于创建 控制机器行为和/或 精确表达算法。 最早的编程语言早于发明 计算机,并被用来指导行为 提花织机和自动钢琴等机器。 已经有成千上万种不同的编程语言 主要是在计算机领域创建的,其中许多是 每年创建。大多数编程语言描述 命令式计算,即作为一个序列 命令,尽管某些语言,例如那些 支持函数式编程或逻辑编程,使用 其他形式的描述。
我想将每一行读入它自己的字符串数组元素中。
我确实有另一个函数成功地使用 getline() 一次显示文本文件的每一行 10 行。
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();
}
【问题讨论】:
-
您以哪种格式将数据存储在文件中?
-
哦,对了。文件的每一行都是包含空格的文本行。类似于:编程语言是一种人工语言,旨在将指令传达给机器,尤其是计算机。编程语言可用于创建控制机器行为和/或精确表达算法的程序。
-
我不认为以下是错误,但您打开文件两次。首先使用构造函数 ifstream ifile(filename);其次使用函数 ifile.open(filename);.
-
好的。谢谢,我会解决的。
-
似乎是启动调试器的理想方案
标签: c++ arrays string file getline