C++ 中使用文件流的文件处理

原文:https://www.studytonight.com/cpp/file-streams-in-cpp.php

文件表示用于存储数据或信息的存储介质。流指的是字节序列。在文件中,我们永久存储数据,即文本或二进制数据,并通过传输字节数据以输入输出操作的形式使用这些数据进行读取或写入。所以我们使用术语文件流/文件处理。我们使用头文件<fstream>

  • ofstream: 表示输出流,用于写入文件。
  • ifstream: 表示输入流,用于读取文件。
  • 流:它代表输出流和输入流。所以它可以从文件中读取和写入文件。

文件处理中的操作:

  • 创建文件:open()
  • 读取数据:read()
  • 写入新数据:write()
  • 关闭文件:close()

创建/打开文件

我们通过指定文件的新路径和操作模式来创建/打开文件。操作可以是读取、写入、追加和截断。文件创建语法:FilePointer.open("Path",ios::mode);

  • 打开进行写入的文件示例:st.open("E:\studytonight.txt",ios::out);
  • 打开读取的文件示例:st.open("E:\studytonight.txt",ios::in);
  • 为追加打开的文件示例:st.open("E:\studytonight.txt",ios::app);
  • 为截断而打开的文件示例:st.open("E:\studytonight.txt",ios::trunc);
#include<iostream>
#include<conio>
#include <fstream>

using namespace std;

int main()
{
    fstream st; // Step 1: Creating object of fstream class
    st.open("E:\studytonight.txt",ios::out);  // Step 2: Creating new file
    if(!st) // Step 3: Checking whether file exist
    {
        cout<<"File creation failed";
    }
    else
    {
        cout<<"New file created";
        st.close(); // Step 4: Closing file
    }
    getch();
    return 0;
}

写入文件

#include <iostream>
#include<conio>
#include <fstream>

using namespace std;

int main()
{
    fstream st; // Step 1: Creating object of fstream class
    st.open("E:\studytonight.txt",ios::out);  // Step 2: Creating new file
    if(!st) // Step 3: Checking whether file exist
    {
        cout<<"File creation failed";
    }
    else
    {
        cout<<"New file created";
        st<<"Hello";    // Step 4: Writing to file
        st.close(); // Step 5: Closing file
    }
    getch();
    return 0;
}

这里我们发送输出到一个文件。所以,我们使用 ios::out。如程序中所给,在“文件指针<<“后的引号内键入的信息将被传递到输出文件。


从文件中读取

#include <iostream>
#include<conio>
#include <fstream>

using namespace std;

int main()
{
    fstream st; // step 1: Creating object of fstream class
    st.open("E:\studytonight.txt",ios::in);   // Step 2: Creating new file
    if(!st) // Step 3: Checking whether file exist
    {
        cout<<"No such file";
    }
    else
    {
        char ch;
        while (!st.eof())
        {
            st >>ch;  // Step 4: Reading from file
            cout << ch;   // Message Read from file
        }
        st.close(); // Step 5: Closing file
    }
    getch();
    return 0;
}

这里我们从一个文件中读取输入。所以,我们使用 ios::in。如程序中所给,输出文件中的信息是借助以下语法“文件指针> >变量”获得的。


关闭文件

FilePointer.close()完成。

#include <iostream>
#include<conio>
#include <fstream>

using namespace std;

int main()
{
    fstream st; // Step 1: Creating object of fstream class
    st.open("E:\studytonight.txt",ios::out);  // Step 2: Creating new file
    st.close(); // Step 4: Closing file
    getch();
    return 0;
}

文件中的特殊操作

很少有重要功能可用于文件流,例如:

  • tellp() - It tells the current position of the put pointer.

    语法: filepointer.tellp()

  • tellg() - It tells the current position of the get pointer.

    语法: filepointer.tellg()

  • seekp() - It moves the put pointer to mentioned location.

    语法: filepointer.seekp(字节数,引用模式)

  • seekg() - It moves get pointer(input) to a specified location.

    语法: filepointer.seekg((字节数,参考点)

  • put() -它将单个字符写入文件。

  • get() -从文件中读取单个字符。

*注:对于 seekp 和 seekg 传递三个参考点: ios::beg -文件开始 ios::cur -文件中当前位置 ios::end -文件结束*

下面是一个展示tellptellgseekpseekg重要性的程序:

#include <iostream>
#include<conio>
#include <fstream>

using namespace std;

int main()
{
    fstream st; // Creating object of fstream class
    st.open("E:\studytonight.txt",ios::out);  // Creating new file
    if(!st) // Checking whether file exist
    {
        cout<<"File creation failed";
    }
    else
    {
        cout<<"New file created"<<endl;
        st<<"Hello Friends"; //Writing to file

        // Checking the file pointer position
        cout<<"File Pointer Position is "<<st.tellp()<<endl;  

        st.seekp(-1, ios::cur); // Go one position back from current position

        //Checking the file pointer position
        cout<<"As per tellp File Pointer Position is "<<st.tellp()<<endl; 

        st.close(); // closing file
    }
    st.open("E:\studytonight.txt",ios::in);   // Opening file in read mode
    if(!st) //Checking whether file exist
    {
        cout<<"No such file";
    }
    else
    {
        char ch;
        st.seekg(5, ios::beg);  // Go to position 5 from begning.
        cout<<"As per tellg File Pointer Position is "<<st.tellg()<<endl; //Checking file pointer position
        cout<<endl;
        st.seekg(1, ios::cur); //Go to position 1 from beginning.
        cout<<"As per tellg File Pointer Position is "<<st.tellg()<<endl; //Checking file pointer position
        st.close(); //Closing file
    }
    getch();
    return 0;
}

新创建的文件文件指针位置为 13 根据文件指针位置为 12 根据文件指针位置为 5 根据文件指针位置为 6