小编use*_*748的帖子

C++ ofstream没有写入输出文件?

该代码应该计算输入文本文件中的a,b,c,d,e和f字符的数量,并将输出打印到第二个文本文件中.当我运行代码时,它会创建输出文件,但不会在其中写入任何内容.

#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;


int main(){

// establish counters for the number of each character
char x;
int acount=0;
int bcount=0;
int ccount=0;
int dcount=0;
int ecount=0;
int fcount=0;

 ifstream iFile("plato.txt"); //define & open files
 ofstream oFile("statistics.txt");

 if(!iFile){
  cout<<"The file could not be opened.";
  exit(1);
 }

 if(!oFile){
  cout<<"The file could not be opened.";
  exit(1);
 }

 iFile>>x;

 while(!iFile.eof()){
  if(x=='a'||x=='A'){
   acount++;
  }
  else if(x=='b'||x=='B'){
   bcount++;
  }
  else if(x=='c'||x=='C'){
   ccount++;
  }
  else if(x=='d'||x=='D'){
   dcount++;
  }
  else if(x=='d'||x=='D'){
   dcount++;
  }
  else …
Run Code Online (Sandbox Code Playgroud)

c++ fstream ofstream

-1
推荐指数
1
解决办法
2513
查看次数

标签 统计

c++ ×1

fstream ×1

ofstream ×1