C++ 读取文件并向后显示
我正试图编写一个程序,从文件中读取并向后显示文本。-我的循环反向循环不工作。有什么建议吗? -另外,如果我正在读取一个只包含浮点整数或浮点的文件,我将如何将它们全部显示为浮点 谢谢C++ 读取文件并向后显示,c++,C++,我正试图编写一个程序,从文件中读取并向后显示文本。-我的循环反向循环不工作。有什么建议吗? -另外,如果我正在读取一个只包含浮点整数或浮点的文件,我将如何将它们全部显示为浮点 谢谢 #include <iostream> #include <fstream> using namespace std; void seeReverseText(char fileName[]) { ifstream fin(fileName); if (fin.fail())
#include <iostream>
#include <fstream>
using namespace std;
void seeReverseText(char fileName[])
{
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error opening file " << fileName << endl;
return;
}
cout.setf(ios::showpoint);
cout.precision(2);
cout.setf(ios::fixed);
int i = 0;
cout << "New order:\n";
while (!fin.eof())
{
// this is what I was trying to do
// i++;
// for (i--; i >= 0; i--)
// fin >> fileName[i];
// cout << "' " << fileName[i] << "'";
fin >> fileName;
cout << fileName << endl;
}
fin.close();
}
int main()
{
char fileName[256];
cout << "Enter the filename: ";
cin >> fileName;
seeReverseText(fileName);
return 0;
}
#包括
#包括
使用名称空间std;
void请参见ReverseText(字符文件名[])
{
ifstream-fin(文件名);
if(fin.fail())
{
不能尝试更像这样的方法:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
void seeReverseText(const std::string &fileName)
{
std::ifstream fin(fileName);
if (!fin)
{
std::cout << "Error opening file " << fileName << std::endl;
return;
}
std::cout.setf(std::ios::showpoint);
std::cout.precision(2);
std::cout.setf(std::ios::fixed);
std::cout << "New order:\n";
std::string line;
while (std::getline(fin, line))
{
std::reverse(line.begin(), line.end());
std::cout << line << std::endl;
}
}
int main()
{
std::string fileName;
std::cout << "Enter the filename: ";
if (std::cin >> fileName)
seeReverseText(fileName);
return 0;
}
#包括
#包括
#包括
#包括
void seeReverseText(常量std::字符串和文件名)
{
std::ifstream-fin(文件名);
如果(!fin)
{
std::cout也提出了类似的问题。char filename[]是指针,不是吗?所以它不是数组,所以您无法访问它的最后一项否?另外,eof()
在您尝试先读取某些内容之前不会更新,所以使用while(!eof)
循环是错误的。