c++文件流

1.写文件ofstream,读文件ifstream,同时读写文件fstream
2.通过创建文件对象来在文件流中操作文件。
3.打开一个文件有以下几种方式:

ios::out 为输出(写)而打开文件
ios::ate 初始位置:文件尾
ios::app 所有输出附加在文件末尾
ios::trunc 如果文件已存在则先删除该文件
ios::binary 二进制方式

4.创建不同类来创建文件对象会有不同的默认打开方式:(可以在调用时修改打开方式)

ofstream ios::out | ios::trunc//由于是写文件流,因此默认输出或者覆盖原同名文件方式创建一个文件
ifstream ios::in//读文件流,以in方式打开
fstream ios::in | ios::out//默认读或者写方式打开

5.创建对象时构造函数本身也可以直接调用open函数(对三种文件流都是)

ofstream file("1.bin");file.open("1.bin",ios::out)
ofstream file("1.bin",ios::out);//两种方式相同
file.is_open()//可以用于判断是否已经打开

6.关闭文件
file.close()//关闭文件,当流对象析构时会自动调用close函数
7.对于文件的输入输出

fstream file("1.txt");
file<<"这是写入文件的操作";
int i;
file>>i;//这是读取一个整数大小的文件
ifstream file("1.txt");
char arr[10];
while (!file.eof())//eof()表示文件的结尾
    {
        examplefile.getline(arr,100);//getline(输入到哪个数组,读取的字符个数)
    }

下面是练习使用的代码,涉及到c++字符串以及文件流的操作,批量创建文件以及通过bat文件来执行批量删除文件,通过读取当前文件的路径来对当前的文件下所有某一类文件进行删除。

#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
#include <unistd.h>
#include <istream>
using namespace std;
// 检测文件大小的程序
long testSize(const char *filename)
{
    long l, r;
    ifstream file(filename);
    r = file.tellg();
    file.seekg(0, ios::end);
    l = file.tellg();
    file.close();
    // cout << "当前文件大小为" << (l - r) / 1024 << "KB";
    return (l - r);
}
// 生成文件的程序
void createAfile(const char *filename, string txt = "this is a txt",int nums=1)
{
    string rename;
    string tail;
    ofstream file(filename);
    if (file.is_open())
    {
        // cout << "文件已经打开";
        file << txt;
    }
    int offset=0;
    while(*(filename+offset)!='.'&&*(filename+offset)!=0)
    {
        rename+=*(filename+offset);
        offset++;
    }
    while(*(filename+offset)!=0)
    {
         tail+=*(filename+offset);
         offset++;
    }
    file.close();
    while(nums>1)
    {
        // cout<<"new file:"<<filename<<"is created!"<<endl;
        file.open(rename+to_string(nums-1)+tail);
        if(file.is_open())
        file<<txt;
        file.close();
        nums--;
    }
    cout<<"finished!......"<<endl;
}
// 缓存中读或者写入文件
void bufferOp(const char *filename)
{
    char *buffer;
    long size;
    ifstream file(filename);
    size = testSize(filename);
    buffer = new char[size];
    file.read(buffer, size);
    file.close();
    delete[] buffer;
}
// 希望写出一个可以自动帮助我删除所有exe文件的程序
void deleteexeFile(string filename, string tail = "*.txt", char *path = nullptr)
{
    // remove(filename);
    if (path == nullptr)
    {
        path = getcwd(NULL, 0);
        puts(path);
        // cout << "path:" << path;
    }
    ofstream file(filename);
    string pah = "\"";
    for (int i = 0; i < 255; i++)
    {
        if (*(path + i) == '\000')
            break;
        pah.push_back(*(path + i));
    }
    pah += "\\" + tail + "\"";
    if (file.is_open())
    {
        cout << "the file is opened succefully" << endl;
        file << "del /a /f /s /q " + pah;
        // cout<<"路径"<<*path;
        // cout << "finished";
    }
    free(path);
    cout << "The file with " << tail << " is removed!" << endl;
}
void exethedat(const char *filename)
{
    system(filename);
}
int main()
{
    string tail;
    string path;
    int nums;
    string iscreateAfile;
    string filename;
    string txt;
    cout << setw(85) << setfill(' ') << "---the program is used for deleting some files---" << setfill('-') << endl
         << "Do you want to create a file?(1/0):";
    cin >> iscreateAfile;
    if (iscreateAfile=="1")
    {
        cout<<"your filename:";
        cin>>filename;
        cout<<"your content:";
        cin>>txt;
        cout<<"your nums:";
        cin>>nums;
        createAfile(filename.c_str(), txt,nums);
    }
    cout << endl
         << "the tail name of the file :";
    cin >> tail;
    cout << endl
         << "the path of the file(if using current path,please input -1):";
    cin >> path;
    if (path != "-1")
    {
        char *k = (char *)malloc((path.size() + 1) * sizeof(char));
        deleteexeFile("d.dat", tail);
    }
    else
        deleteexeFile("d.bat", tail);
    exethedat("d.bat");
    remove("d.bat");
    system("pause");
}
posted @ 2022-01-21 19:37  梦呓qwq  阅读(75)  评论(0编辑  收藏  举报