如何从一段时间开始打开和关闭文件

How to open and close a file from a while

本文关键字:文件 一段时间 开始      更新时间:2023-10-16

我需要这个程序来显示文件计数(文本文档)。但是,不重置单词的反词。它应该是7,但显示62个字。有人告诉我要关闭第二个文件,并创建一个新的时(是最后一个),然后再次打开文件。我已经为此努力了一段时间。但是,进行更改后,它将不再显示文件。屏幕上唯一显示的东西是2个单词的帐户。 感谢您的帮助

#include<iostream>
#include<fstream>//step#1
#include<string>
using namespace std;
int main()
{
   string word,fileName;
   char character;
   int charcounter = 0, wordcounter = 0;
   ifstream inData;// incoming file stream variable
   cout << " Enter filename or type quit to exit: ";
   cin >> fileName;
   //loop to allow for multiple files data reads
   while (fileName != "quit")
   {    
      inData.open(fileName.c_str());//open file and bind file to ifstream variable
      //loop for file not found validation
      while (!inData)//filestream is in fail state due to no file
      {
         inData.clear();//clear the fail state 
         cout <<"File not found. Enter the correct filename: ";
         cin >> fileName;
         inData.open(fileName.c_str());
      }
      inData >> character;//extract a single character from the file
      cout << "n*****************************n";
      while (inData)
      {
         cout << character;
         inData.get(character); 
         charcounter++;
      }
      cout << "n******************************n";
      cout << fileName << " has " << charcounter << " words." << endl;
      inData.close();//close the ifstream conection to the data file
      while (inData)
      {
         cout << word;
         wordcounter++;
         inData.open(fileName.c_str());
      }
      charcounter = 0; //reset character count
      wordcounter = 0;
      //port for next file or exit
      cout << "Enter a filename or type quit to exit: ";
      cin >> fileName;
   }
   return 0;
}

您想要这个吗?

#include<iostream>
#include<fstream>//step#1
#include<string>
using namespace std;
int main() {
   string word,fileName;
   char character;
   int charcounter = 0, wordcounter = 0;
   ifstream inData;// incoming file stream variable
   cout << " Enter filename or type quit to exit: ";
   cin >> fileName;
   //loop to allow for multiple files data reads
   while (fileName != "quit")
   {
      inData.open(fileName.c_str());//open file and bind file to ifstream variable
      //loop for file not found validation
      while (!inData)//filestream is in fail state due to no file
      {
         inData.clear();//clear the fail state
         cout <<"File not found. Enter the correct filename: ";
         cin >> fileName;
         inData.open(fileName.c_str());
      }

      cout << "n*****************************n";
      while (inData)
      {
         inData >> character;//extract a single character from the file
         cout << character;
         charcounter++;
      }
      cout << "n******************************n";
      cout << fileName << " has " << charcounter << " chars." << endl;
      inData.close();//close the ifstream conection to the data file
      inData.open(fileName.c_str());
      while (inData)
      {
         inData >> word;
         cout << word;
         wordcounter++;
      }
      cout << "n******************************n";
      cout << fileName << " has " << wordcounter << " words." << endl;
      inData.close();//close the ifstream conection to the data file

      charcounter = 0; //reset character count
      wordcounter = 0;
      //port for next file or exit
      cout << "Enter a filename or type quit to exit: ";
      cin >> fileName;
   }
   return 0;
}