用C++编写相关程序12

请编写程序,上机调试,题目如下图,请按题目要求编写,并给出程序

img


参考程序如下:

#include 
#include 
#include 
using namespace std;
void main()
{
    char b[10];
    ofstream outfile("1.txt");
     if (!outfile){ cout<<“open failed!” ; return ; }

    outfile<<“abcd”; //写入一个字符串
    outfile.close();

      ifstream ifile("1.txt");
      if(!ifile ){ cout<<“open failed!” ; return ; }

      ifile.getline(&b,10); //按照行读取,最多10个字符
     cout<<"b="<< b<close();
}



#include <iostream>
#include <fstream>

using namespace std;
void main()
{
  char b[1024];

  //读文件
  ifstream ifile("1.txt");
  if (!ifile)
  { 
    cout << "open failed!"; 
    return; 
  }

  ifile.getline(b, 1024); //按照行读取,最多10个字符
  cout << "b=" << b<<std::endl;

  //反转字符串
  int len = strlen(b);
  int i = 0;
  while (i < len && (i + 3) < len)
  {
    char c1 = b[i];
    char c2 = b[i + 1];
    char c3 = b[i + 2];
    char c4 = b[i + 3];
    b[i + 3] = c1;
    b[i + 2] = c2;
    b[i + 1] = c3;
    b[i] = c4;
    i = i + 4;
  }
  cout << "反转后:b=" << b << std::endl;

  ofstream outfile("2.txt");
  if (!outfile) 
  { 
    cout << "open failed!";
    return; 
  }

  outfile << b; //写入一个字符串
  outfile.close();
}

img

ofstream是输出到文件的文件流,ifstream才是读入的流,你搞反了,麻烦采纳一下哟,谢谢^-^