文件操作

增加2~3个文件操作命令,并加以实现。(如移动读写指针,改变文件属性,更换文件名,改变文件保护级别)

代码:

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

string name,content;
string judge;
string fname,newname;
string file;

int main()
{
    cout << "1. write (append or write)"<<endl;
    cout << "2. rename (rename files)" << endl;
    cout << "3. list (list files)" << endl;
    cout << "4. chmod (change mode of files)" << endl;
    cout << "5. exit (exit)"<<endl;
    cout << "Enter order:";
    int cmd;
    while(cin >> cmd)
    {
        if(cmd == 1)
        {
            cout << "Input the name of the file(*.txt):";
            cin >> name;
            FILE *fp;
            cout << "Choose the mood(w or w+):";
            cin >> judge;
            if(judge == "w")
            {
                if((fp=fopen(name.c_str(),"w"))!= NULL)
                    puts("open successfully");
                else
                {
                    puts("Fail to open");
                    break;
                }
            }
            else if(judge == "w+")
            {
                if((fp=fopen(name.c_str(),"w+"))!=NULL)
                    puts("open successfully");
                else
                {
                    puts("Fail to open");
                    break;
                }
            }
            else
            {
                cout << "Don't exist" << endl;
            }

            cout << "Enter the content:";
            cin >> content;

            fputs(content.c_str(),fp);

            fclose(fp);
        }
        else if(cmd == 2)
        {
            cout << "Enter the old name:" << endl;
            cin >> fname;
            cout << "New name:";
            cin >> newname;
            int result;
            result = rename(fname.c_str(),newname.c_str());
            if(result == 0)
            {
                puts("Rename Successfully!");
            }
            else
            {
                puts("Fail to rename");
            }
        }
        else if(cmd == 3)
        {
            cout << "enter file name or all files(*):";
            cin >> fname;
            if(fname == "*")
            {
                system("attrib *");
            }
            else
            {
                string tmp = "attrib " + fname;
                system(tmp.c_str());
            }
        }
        else if(cmd == 4)
        {
            long file;
            struct _finddata_t find;

            if((file=_findfirst("*.*",&find))==-1L)
            {
                printf("NULL\n");
                exit(0);
            }

            printf("%s\n",find.name);
            while(_findnext(file,&find) == 0)
            {
                printf("%10s\t%5d\t%d\n",find.name,find.size,find.attrib);
            }
            _findclose(file);
        }
        else if(cmd == 5)
        {
            break;
        }
        cout << "Enter your order:";
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值