void find(char* obj,int &flag) { for (int i = 2; i < MSD + 2; i++) { //如果是有效文件,则检查是否有目标内容 if (curDir->directItem[i].firstDisk != -1&&curDir->directItem[i].type == 0&&curDir->directItem[i].size!=0&& strcmp(curDir->directItem[i].fileName,"")!=0) { char* buf = new char[MAX_FILE_SIZE]; string newStr[2]; newStr[0] = "more"; newStr[1] = curDir->directItem[i].fileName; read(newStr, buf); if (strstr(buf, obj) != NULL) { cout <<"# "<< curPath << newStr[1] << "中有目标内容!" << endl; flag = 1; } delete[] buf; } //如果是目录,则递归查找子目录下文件内容 if(curDir->directItem[i].firstDisk != -1 && curDir->directItem[i].type == 1&&strcmp(curDir->directItem[i].fileName, "") != 0) { string dir = curDir->directItem[i].fileName; direct* cur = curDir; string oldPath = curPath; curDir = (direct*)(fdisk + curDir->directItem[i].next * DISK_SIZE); curPath = curPath + dir + "\\"; find(obj,flag); curPath = oldPath; curDir = cur; } } }代码解释
时间: 2024-03-28 07:40:55 浏览: 51
这段代码是一个递归函数,用于在当前目录及其子目录下查找包含特定内容的文件。
函数接受两个参数:一个指向要查找的内容的 char 指针,另一个是一个引用变量,用于标记是否找到目标内容。
在函数中,循环遍历当前目录下的所有文件和子目录。对于每个文件,如果它是一个有效的文件(即不是空文件,且文件名不为空),则读取其内容,并检查其是否包含目标内容。如果找到目标内容,则将标记变量设为 1。
对于每个子目录,递归调用函数,以在该子目录下查找目标内容。在递归调用之前,保存当前目录和路径信息,以便在递归调用结束后返回到原始目录。
相关问题
设计一个模板类SET,集合中可以存放不同类型的对象;然后编写一个演示程序利用SET创建并使用整数的集合、字符串的集合以及学生的集合,学生类STUDENT是自己设计的类类型,包括对学生的学号、姓名、年龄、系别等信息的描述及必要的行为。(提示:集合中的元素不允许重复,且元素之间是无序的)SET类的原型可参考如下:template <class T>class SET {private:T elements[MAX_LEN];// 定义元素集合,来记录SET中的元素intN;// 定义元素的实际个数public:SET(); SET(T data[], int n);SET(const SET &obj);~SET();int hasElement(T one); //判断元素one是否在集合中void inputElement(T one); // 向集合中加入一个元素void delElement(T one);// 从集合中删除一个元素void showAllElements(); //显示集合中的所有元素// SET对象之间的运算:交、并、差,分别采用运算符重载实现SET operator+(const SET &obj);SET operator*(const SET &obj);SET operator-(const SET &obj);SET operator=(const SET &obj);};STUDENT类的原型可参考如下:class STUDENT{private:long id;char name[MAX_LEN];char sex;int age;public:STUDENT();STUDENT(const STUDENT& obj);STUDENT(long idd, char namee[], char sexx, int agee);~STUDENT();int operator==(const STUDENT& obj);STUDENT operator=(const STUDENT& obj);// 定义友元函数friend ostream& operator<<(ostream& stream, const STUDENT& obj);};题目说明:1)SET中的数据可以使用固定长度的数组存储,也可以使用动态数组存储,也可
以其他方式存储;
2)在演示程序中,需要创建整数、字符串和学生的集合,并进行交、并、差等运算;
3)在STUDENT类中,需要重载==运算符和=运算符,并定义友元函数ostream& operator<<(ostream& stream, const STUDENT& obj),用于输出学生信息。
下面是一个可能的实现:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_LEN = 100;
template <class T>
class SET {
private:
T elements[MAX_LEN];
int N;
public:
SET() {
N = 0;
}
SET(T data[], int n) {
N = 0;
for (int i = 0; i < n; i++) {
inputElement(data[i]);
}
}
SET(const SET &obj) {
N = obj.N;
for (int i = 0; i < N; i++) {
elements[i] = obj.elements[i];
}
}
~SET() {}
int hasElement(T one) {
for (int i = 0; i < N; i++) {
if (elements[i] == one) {
return i;
}
}
return -1;
}
void inputElement(T one) {
if (hasElement(one) == -1) {
elements[N++] = one;
}
}
void delElement(T one) {
int index = hasElement(one);
if (index != -1) {
for (int i = index; i < N - 1; i++) {
elements[i] = elements[i + 1];
}
N--;
}
}
void showAllElements() {
for (int i = 0; i < N; i++) {
cout << elements[i] << " ";
}
cout << endl;
}
SET operator+(const SET &obj) {
SET result;
for (int i = 0; i < N; i++) {
result.inputElement(elements[i]);
}
for (int i = 0; i < obj.N; i++) {
result.inputElement(obj.elements[i]);
}
return result;
}
SET operator*(const SET &obj) {
SET result;
for (int i = 0; i < N; i++) {
if (obj.hasElement(elements[i]) != -1) {
result.inputElement(elements[i]);
}
}
return result;
}
SET operator-(const SET &obj) {
SET result;
for (int i = 0; i < N; i++) {
if (obj.hasElement(elements[i]) == -1) {
result.inputElement(elements[i]);
}
}
return result;
}
SET operator=(const SET &obj) {
N = obj.N;
for (int i = 0; i < N; i++) {
elements[i] = obj.elements[i];
}
return *this;
}
};
class STUDENT {
private:
long id;
char name[MAX_LEN];
char sex;
int age;
public:
STUDENT() {}
STUDENT(const STUDENT& obj) {
id = obj.id;
strcpy(name, obj.name);
sex = obj.sex;
age = obj.age;
}
STUDENT(long idd, char namee[], char sexx, int agee) {
id = idd;
strcpy(name, namee);
sex = sexx;
age = agee;
}
~STUDENT() {}
int operator==(const STUDENT& obj) {
return (id == obj.id && strcmp(name, obj.name) == 0 && sex == obj.sex && age == obj.age);
}
STUDENT operator=(const STUDENT& obj) {
id = obj.id;
strcpy(name, obj.name);
sex = obj.sex;
age = obj.age;
return *this;
}
friend ostream& operator<<(ostream& stream, const STUDENT& obj) {
stream << obj.id << " " << obj.name << " " << obj.sex << " " << obj.age;
return stream;
}
};
int main() {
SET<int> intSet1, intSet2;
intSet1.inputElement(1);
intSet1.inputElement(2);
intSet1.inputElement(3);
intSet2.inputElement(2);
intSet2.inputElement(3);
intSet2.inputElement(4);
SET<int> intSet3 = intSet1 + intSet2;
SET<int> intSet4 = intSet1 * intSet2;
SET<int> intSet5 = intSet1 - intSet2;
cout << "Int Set 1: ";
intSet1.showAllElements();
cout << "Int Set 2: ";
intSet2.showAllElements();
cout << "Int Set 1 + Int Set 2: ";
intSet3.showAllElements();
cout << "Int Set 1 * Int Set 2: ";
intSet4.showAllElements();
cout << "Int Set 1 - Int Set 2: ";
intSet5.showAllElements();
SET<string> stringSet1, stringSet2;
stringSet1.inputElement("hello");
stringSet1.inputElement("world");
stringSet2.inputElement("world");
stringSet2.inputElement("!");
SET<string> stringSet3 = stringSet1 + stringSet2;
SET<string> stringSet4 = stringSet1 * stringSet2;
SET<string> stringSet5 = stringSet1 - stringSet2;
cout << "String Set 1: ";
stringSet1.showAllElements();
cout << "String Set 2: ";
stringSet2.showAllElements();
cout << "String Set 1 + String Set 2: ";
stringSet3.showAllElements();
cout << "String Set 1 * String Set 2: ";
stringSet4.showAllElements();
cout << "String Set 1 - String Set 2: ";
stringSet5.showAllElements();
STUDENT student1(1, "Alice", 'F', 20), student2(2, "Bob", 'M', 21), student3(3, "Charlie", 'M', 22);
SET<STUDENT> studentSet1, studentSet2;
studentSet1.inputElement(student1);
studentSet1.inputElement(student2);
studentSet2.inputElement(student2);
studentSet2.inputElement(student3);
SET<STUDENT> studentSet3 = studentSet1 + studentSet2;
SET<STUDENT> studentSet4 = studentSet1 * studentSet2;
SET<STUDENT> studentSet5 = studentSet1 - studentSet2;
cout << "Student Set 1: ";
studentSet1.showAllElements();
cout << "Student Set 2: ";
studentSet2.showAllElements();
cout << "Student Set 1 + Student Set 2: ";
studentSet3.showAllElements();
cout << "Student Set 1 * Student Set 2: ";
studentSet4.showAllElements();
cout << "Student Set 1 - Student Set 2: ";
studentSet5.showAllElements();
return 0;
}
```
阅读全文