Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用C++;?_C++_File_Reverse_Reversing - Fatal编程技术网

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