C++ C++;如何读入文本文件并反转行,使其自下而上读取
请帮帮我,我是C的新手++ 使用堆栈实现 我应该阅读包含以下内容的示例文本文件:C++ C++;如何读入文本文件并反转行,使其自下而上读取,c++,C++,请帮帮我,我是C的新手++ 使用堆栈实现 我应该阅读包含以下内容的示例文本文件: text file begining to end return text 并返回一个文本文件,读取: return text begining to end test file 这是测试实现的示例代码: #include <iostream> #include <fstream> #include <string> #include "stack1.h" using nam
text file
begining to end
return text
并返回一个文本文件,读取:
return text
begining to end
test file
这是测试实现的示例代码:
#include <iostream>
#include <fstream>
#include <string>
#include "stack1.h"
using namespace std
void reverse (ifstream & infile, ofstream & outfile); //function prototype
int main () {
ifstream infile; // declaring a file for input
ofstream outfile; // declaring a file for output
infile.open ("c:\\myInputFile.txt");
if (!infile) { cout << "File could not be opened. Program terminated." << endl;
return 1; // terminates program
}
outfile.open ("c:\\myOutfile.txt");
reverse (infile, outfile);
infile.close();
outfile.close();
return 0;
}
void reverse (ifstream & infile, ofstream & outfile) {
string s;
stack<string> mystack;
getline(infile, s); // try to read a line of text from the file
while (!infile) { // while not end of file
mystack.push(s);
getline(infile, s); // try to read another line
}
while (! mystack.isEmpty() ) {
s = mystack.pop();
outfile << s << endl; // write s to the outfile
}
}
#包括
#包括
#包括
#包括“stack1.h”
使用名称空间std
孔隙反转(如果水流和填充、水流和流出)//功能原型
int main(){
ifstream infle;//声明要输入的文件
ofstream outfile;//声明要输出的文件
infle.open(“c:\\myInputFile.txt”);
如果(!infle){cout
//open infile in and outfile out.
std::stack<std::string> lines;
std::string temp;
while(std::getline(in, temp)) lines.push(temp);
while(!lines.empty()) outfile << lines.pop() << std::endl;
//打开内嵌和导出文件。
std::堆栈线;
std::字符串温度;
while(std::getline(in,temp))line.push(temp);
而(!lines.empty())outfile如何:
//open infile in and outfile out.
std::stack<std::string> lines;
std::string temp;
while(std::getline(in, temp)) lines.push(temp);
while(!lines.empty()) outfile << lines.pop() << std::endl;
//打开内嵌和导出文件。
std::堆栈线;
std::字符串温度;
while(std::getline(in,temp))line.push(temp);
虽然(!lines.empty())输出文件摆脱!
while (infile) { // while not end of file
您可以使用std::stack
而不是“stack1.h”中的任何内容摆脱!
while (infile) { // while not end of file
您可以使用std::stack
而不是“stack1.h”
检查此选项,您可以直接存储它而不使用stack。我认为它应该可以工作
int checkstatus(ifstream &in)
{
ios::iostate i;
i = in.rdstate();
if(i & ios::eofbit)
return 0;//cout << "EOF encountered\n";
else if(i & ios::failbit)
return 0;//cout<<"Non-Fatal I/O error\n";
else if(i & ios::badbit)
return 0;//cout<<"Fatal I/O error\n";
return 1;
}
int main()
{
ifstream in;
in.open("test.txt");
char c;
in.seekg(0,ios::end);
while(checkstatus(in) != 0)
{
in.seekg(-1,ios::cur);
in.get(c);
in.seekg(-1,ios::cur);
if(checkstatus(in) == 0)
break;
cout<<c;
}
return 0;
}
int checkstatus(ifstream&in)
{
ios::iostateⅠ;
i=处于.rdstate();
if(i&ios::eofbit)
返回0;//cout检查此选项,您可以不使用堆栈直接存储它。我认为它应该可以工作
int checkstatus(ifstream &in)
{
ios::iostate i;
i = in.rdstate();
if(i & ios::eofbit)
return 0;//cout << "EOF encountered\n";
else if(i & ios::failbit)
return 0;//cout<<"Non-Fatal I/O error\n";
else if(i & ios::badbit)
return 0;//cout<<"Fatal I/O error\n";
return 1;
}
int main()
{
ifstream in;
in.open("test.txt");
char c;
in.seekg(0,ios::end);
while(checkstatus(in) != 0)
{
in.seekg(-1,ios::cur);
in.get(c);
in.seekg(-1,ios::cur);
if(checkstatus(in) == 0)
break;
cout<<c;
}
return 0;
}
int checkstatus(ifstream&in)
{
ios::iostateⅠ;
i=处于.rdstate();
if(i&ios::eofbit)
return 0;//cout@BillyONeal.Thank,还不能编辑。@BillyONeal.Thank,还不能编辑。我同意你的问题是什么。看来你已经编写的代码应该可以运行了,你只需要在最后弹出堆栈并显示它的所有内容就可以反向查看文件。这不是用VS2008编译的。lines.pop()
根据MSDN返回void
。我必须使用行。top()
@Agnel:Doh!习惯于使用普通向量,因为堆栈只是向量顶部的适配器。@Jerry:谢谢修复代码。我同意你的问题。看来你已经编写的代码应该可以运行了,你只需要在最后弹出堆栈并显示其所有内容即可反向查看文件。这不是用VS2008编译的。lines.pop()
根据MSDN返回void
。我不得不使用lines.top()
@Agnel:Doh!习惯于使用普通向量,因为堆栈只是向量顶部的适配器。@Jerry:感谢修复代码。它通过从EOF中查找每个字符来工作。不必使用堆栈或向量以这种方式复制任何数据。它通过从EOF中查找每个字符来工作。不必使用堆栈或v以这种方式复制任何数据埃克托。