C++ 如何使用C++;?
我需要颠倒文件的顺序并输出到另一个文件中。例如 输入:C++ 如何使用C++;?,c++,file,reverse,reversing,C++,File,Reverse,Reversing,我需要颠倒文件的顺序并输出到另一个文件中。例如 输入: hello this is a testing file again just so much fun 预期产出: just so much fun this is a testing file again hello 这是我当前的代码,它打印到哪里,它颠倒了行的顺序,也颠倒了每个单词的字符顺序 当前: nuf hcum os tsuj niaga elif gnitset a si sih
hello
this is a testing file again
just so much fun
预期产出:
just so much fun
this is a testing file again
hello
这是我当前的代码,它打印到哪里,它颠倒了行的顺序,也颠倒了每个单词的字符顺序
当前:
nuf hcum os tsuj
niaga elif gnitset a si siht
olleh
int print_rev(string filename, char c){
ifstream inStream (filename);
ofstream outStream;
inStream.seekg(0,inStream.end);
int size = inStream.tellg();
outStream.open("output.txt");
for (int j=1; j<=size; j++){
inStream.seekg(-j, ios::end);
c=inStream.get();
outStream << c;
}
inStream.close();
outStream.close();
return 0;
}
nuf hcum os tsuj
尼亚加·艾利夫·格尼斯特
奥利
int print_rev(字符串文件名,字符c){
ifstream inStream(文件名);
流外流;
流内seekg(0,流内端);
int size=inStream.tellg();
exptream.open(“output.txt”);
对于(int j=1;j您正在逐字符反转整个文件。您要做的是分别读取每一行,然后反转行顺序
对于这一点,一堆行似乎是一个不错的选择:
int printRev(string filename)
{
stack<string> lines;
ifstream in(filename);
string line;
while (getline(in, line))
{
lines.push(line);
}
ofstream out("output.txt");
while (!lines.empty())
{
out << lines.top() << endl;
lines.pop();
}
return 0;
}
int-printRev(字符串文件名)
{
叠加线;
ifstream-in(文件名);
弦线;
while(getline(in,line))
{
推(线);
}
流输出(“output.txt”);
而(!lines.empty())
{
你是说预期的输出是“非常有趣,所以再次测试一个文件,这是你好吗?”?该问题需要进行格式编辑,以更好地显示文件内容的逐行性质为什么text
和c
传递到函数中?一个根本不使用,另一个作为本地文件更好。您的设计是什么?您希望您的代码如何实现您想要的功能?此代码是一个函数t主代码将使用hat