#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/ipc.

#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <dirent.h>
#include<cstring>
#include <time.h>
#include <cstdlib>

using namespace std;

struct FileInfo {
    char name[256];      // 文件名
    int permission;      // 存取权限
    int length;          // 文件长度
    time_t createTime;   // 创建时间
    time_t modifyTime;   // 修改时间
};

void dir() {
    cout << "文件目录:" << endl;
    DIR* dir;               // 目录指针
    struct dirent* ent;     // 目录项结构体
    if ((dir = opendir(".")) != NULL) {  // 打开当前目录
        while ((ent = readdir(dir)) != NULL) {  // 读取目录中的每一项
            struct stat st;
            stat(ent->d_name, &st);  // 获取文件信息
            cout << "文件名:" << ent->d_name << endl;
            cout << "存取权限:" << oct << (st.st_mode & 0777) << endl;
            cout << "文件长度:" << st.st_size << " 字节" << endl;
            cout << "创建时间:" << ctime(&st.st_ctime);
            cout << "修改时间:" << ctime(&st.st_mtime);
            cout << "-------------------" << endl;
        }
        closedir(dir);  // 关闭目录
    } else {
        cerr << "无法打开当前目录!" << endl;
    }
}

void create(const char* filename) {
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; //指定权限为644
    FILE* file = fopen(filename, "w");
    if (file != NULL) {
        printf("成功创建文件:%s", filename);
        fclose(file);
        // 设置文件权限
        chmod(filename, mode);
        printf("已将文件%s设置为指定权限", filename);
    } else {
        perror("创建文件失败!");
    }
}

void write(const string& filename) {
    ofstream file(filename.c_str(), ios::app);  // 打开文件进行追加写操作
    if (file) {
        string content;
        cout << "请输入要写入的内容(输入EOF结束):" << endl;
        while (getline(cin, content)) {
            if (content == "EOF") {
                break;
            }
            file << content << endl;
        }
        file.close();
    } else {
        cerr << "打开文件失败!" << endl;
    }
}

void read(const string& filename) {
    ifstream file(filename.c_str());  // 打开文件进行读操作
    if (file) {
        string line;
        while (getline(file, line)) {
            cout << line << endl;
        }
        file.close();
    } else {
        cerr << "打开文件失败!" << endl;
    }
}

void remove(const string& filename) {
    if (remove(filename.c_str()) == 0) {  // 删除文件
        cout << "成功删除文件:" << filename << endl;
    } else {
        cerr << "删除文件失败!" << endl;
    }
}

int main() {
    while (true) {
        printf("________________________欢迎_______________________________\n");
        printf("-----------------------------------------------------------\n");
        printf("************* Welcome to Fu's Linux system! ***************\n\n");
        cout << "请选择操作:1.列文件目录 2.创建文件 3.删除文件 4.读文件 5.写文件 6.退出" << endl;
        int choice;
        cin >> choice;
        cin.ignore(); // 忽略换行符
      string createFilename;  string deleteFilename;  string readFilename;
                 string writeFilename;
        switch (choice) {
            case 1:
                dir();
                break;
            case 2:
                cout << "请输入要创建的文件名:";
                
                getline(cin, createFilename);
                create(createFilename.c_str());
                break;
            case 3:
                cout << "请输入要删除的文件名:";
              
                getline(cin, deleteFilename);
                remove(deleteFilename);
                break;
            case 4:
                cout << "请输入要读取的文件名:";
              getline(cin, readFilename);
                read(readFilename);
                break;
            case 5:
                cout << "请输入要写入的文件名:";
               
                getline(cin, writeFilename);
                write(writeFilename);
                break;
            case 6:
                return 0;
            default:
                cout << "无效的选择!" << endl;
                break;
        }
    }

    return 0;
}

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值