c++代码 哈利波特检索

源代码:


#include <fstream>
#include <deque>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <vector>
#include<stdlib.h>
#include<time.h> 
using namespace std;
bool daxie(string a) 
{
    if (a[0]=='C')
    return true;
    return false;
}
string name, temp, he[10000];
int chapter;
char a[10000];
int index = 0, page = 1, ix;
void magic(string prename, string strname)
{
    page=1;
    chapter=1;
    name=strname;
    ifstream infile;
    string txtname=prename + ".txt";
 infile.open(txtname.c_str());
 if (!infile)
 {
  cerr << "File could not be open!" << endl;
  abort();
 }
 while (infile.getline(a, 10000))
 {
  temp = a;
  if (temp[0] > '0'&&temp[0] <= '9')
   page++;
  if (temp.find("Chapter") != string::npos&&daxie(temp) || temp.find("CHAPTER") != string::npos&&daxie(temp))
  {
   chapter++;
  }
  if (temp.find(name) != string::npos)
  {
   ix = -1;
   while (temp.find(name, ix + 1) != string::npos)
   {
    ix = temp.find(name, ix + 1);
    index++;
    cout << index  << "  " << name << '\t'<< "  " << page << '\t' <<" "<< chapter << '\t'  << prename<< '\t' << endl;
    he[index] = temp;
   }
  }
 }
}


int main()
{cout<<"  "<<endl;
 getline(cin, name);
clock_t startTime,endTime;  startTime = clock();   
 cout << "  " << endl;
 magic("J.K. Rowling - HP 0 - Harry Potter Prequel", name);
 magic("J.K. Rowling - HP 2 - Harry Potter and the Chamber of Secrets", name);
 magic("J.K. Rowling - HP 3 - Harry Potter and the Prisoner of Azkaban", name);
 magic("J.K. Rowling - HP 4 - Harry Potter and the Goblet of  Fire", name);
  magic("J.K. Rowling - HP 6 - Harry Potter and the Half-Blood Prince", name);
 magic("J.K. Rowling - HP 7 - Harry Potter and the Deathly Hallows", name);
 magic("J.K. Rowling - Quidditch Through the Ages", name);
 magic("J.K. Rowling - The Tales of Beedle the Bard", name);

endTime = clock();  cout << '\n'<<"  " <<(double)(endTime - startTime)/CLOCKS_PER_SEC << "s" << endl;   
 int i;
 cout<<"\n  "<< "  " <<(double)(endTime - startTime)/CLOCKS_PER_SEC << "s" << endl;   
 int i;
 cout<<"\n  "<<endl; 
 cin >> i;
 cout << he[i - 1] << he[i] << he[i + 1] << endl;
 system("pause");
 return 0;
}

运行后:

img

为什么😭

三个可能得原因:1、是文件路径没对,看看相对或绝对路径弄对了没有; 2、权限问题:如果文件是以只读或其他限制权限的方式打开的,程序可能无法读取该文件。请确保文件的权限设置允许程序读取文件 3、文件不存在:如果指定的文件不存在,程序将无法打开它。请检查文件是否存在,并确保文件名和扩展名的拼写正确。 仔细检查一下吧