指针问题
真的奇葩
//Main
#include "fStruct.h"
#include "otherHear.h"
#include "menu.h";
int main() {
PFOLDER root;
cout << (initRoot(root) == true ? "创建根目录成功": "创建根目录失败") << "\n";
menuInscreen();
int oprate;
while (cin >> oprate) {
cout << root->name;
switch (oprate) {
case 1 : {
createFFILE(root);
//cout << root->firstChildFolder->name;
break;
}
}
int num = 0;
//PFOLDER root1 = root;
/*while (root != nullptr) {
cout << root->name <<endl;
root = root->firstChildFolder;
cout << 1 << endl;
}*/
//cout << root->name << endl;
//cout << "root地址" << &root << endl;
//cout << num;
}
}
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
//结构体 头
//文件
typedef struct FFILE
{
char name[256]; //文件名
char content[1000]; //文件内容
struct FFILE* frontFile; //同级目录上一文件
struct FFILE* nextFile; //同级目录下一文件
struct FOLDER* parentFolder; //父目录
int canRead; //是否可读
int canWrite; //是否可写
}FFILE, *PFILE;
//文件夹/目录
typedef struct FOLDER
{
char name[256]; //目录名
struct FOLDER* nextFolder; //同级下一目录
struct FOLDER* frontFolder; //同级上一目录
struct FOLDER* parentFolder; //父目录
struct FOLDER* firstChildFolder; //子目录,多个或者一个,要么创建一条链,要么是很树树枝
struct FFILE* firstChildFile; //子文件
int canRead; //是否可读
int canWrite; //是否可写
}FOLDER,* PFOLDER;
//创建目录
void createFFILE(PFOLDER& root) {//参数为父亲目录
FOLDER folder; //第二次调用居然folder等于root的子节点
cout << "文件命名:" << "\n";
cin >> folder.name;
//父目录
folder.parentFolder = root;
if (root->firstChildFolder == nullptr) { // 如果父亲子目录为null直接指向当前目录
cout << "不行";
root->firstChildFolder = &folder;
//同上一级
folder.frontFolder = nullptr;
//同下一级
folder.nextFolder = nullptr;
} else { // 在同级目录的末尾插入一个同级目录
if (root->firstChildFolder == &folder) {
cout << "m" << endl;
}
cout << root->name << endl;
cout << "开始" << root->firstChildFolder->name << endl;
PFOLDER childhead = root->firstChildFolder;
cout << "第二次" << childhead->name;
while (childhead != nullptr && childhead->nextFolder != nullptr) {
childhead = childhead->nextFolder;
cout << "地址" << &childhead << endl;
if (childhead == nullptr) break;
}
//同级下一目录
childhead->nextFolder = &folder;
//同级上一目录
folder.frontFolder = childhead;
//内存清理
delete childhead;
}
//子目录
folder.firstChildFolder = nullptr;
//子文件
folder.firstChildFile = nullptr;
cout << "是否可读1 / 0";
cin >> folder.canRead;
cout << "是否可写1 / 0";
cin >> folder.canWrite;
}
// 创建根目录
bool create(PFOLDER& root) {
strcpy_s(root->name,"/");
root->nextFolder = nullptr;
root->frontFolder = nullptr;
root->parentFolder = nullptr;
root->firstChildFolder = nullptr;
root->firstChildFile = nullptr;
root->canRead = 1;
root->canWrite = 1;
return true;
}
// 初始化
bool initRoot(PFOLDER& root) {
root = new FOLDER();
if (!create(root)) {
return false;
}
return true;
}
用心做~