c#处理文件流_在C ++中使用文件流进行文件处理

c#处理文件流

File represents storage medium for storing data or information. Streams refer to sequence of bytes. In Files we store data i.e. text or binary data permanently and use these data to read or write in the form of input output operations by transferring bytes of data. So we use the term File Streams/File handling. We use the header file <fstream>

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

  • ofstream: It represents output Stream and this is used for writing in files.

    ofstream:表示输出Stream,用于写入文件。

  • ifstream: It represents input Stream and this is used for reading from files.

    ifstream:表示输入Stream,用于读取文件。

  • fstream: It represents both output Stream and input Stream. So it can read from files and write to files.

    fstream:表示输出流和输入流。 因此它可以从文件读取并写入文件。

Operations in File Handling:

文件处理中的操作:

  • Creating a file: open()

    创建文件: open()

  • Reading data: read()

    读取数据: read()

  • Writing new data: write()

    写入新数据: write()

  • Closing a file: close()

    关闭文件: close()

创建/打开文件 (Creating/Opening a File)

We create/open a file by specifying new path of the file and mode of operation. Operations can be reading, writing, appending and truncating. Syntax for file creation: FilePointer.open("Path",ios::mode);

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

  • Example of file opened for writing: st.open("E:\studytonight.txt",ios::out);

    打开的可写文件示例: st.open("E:\studytonight.txt",ios::out);

  • Example of file opened for reading: st.open("E:\studytonight.txt",ios::in);

    打开供读取的文件示例: st.open("E:\studytonight.txt",ios::in);

  • Example of file opened for appending: st.open("E:\studytonight.txt",ios::app);

    打开的用于追加文件的示例: st.open("E:\studytonight.txt",ios::app);

  • Example of file opened for truncating: st.open("E:\studytonight.txt",ios::trunc);

    打开的要截断的文件示例: 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;
}

写入文件 (Writing to a File)

#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;
}

Here we are sending output to a file. So, we use ios::out. As given in the program, information typed inside the quotes after "FilePointer <<" will be passed to output file.

在这里,我们将输出发送到文件。 因此,我们使用ios :: out。 如程序中所给,将在“ FilePointer <<”之后的引号内键入的信息传递给输出文件。

从文件读取 (Reading from a File)

#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;
}

Here we are reading input from a file. So, we use ios::in. As given in the program, information from the output file is obtained with the help of following syntax "FilePointer >>variable".

在这里,我们从文件读取输入。 因此,我们使用ios :: in。 如程序中所给出的,借助于以下语法“ FilePointer >> variable”从输出文件中获取信息。

关闭档案 (Close a File)

It is done by FilePointer.close().

它由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;
}

文件中的特殊操作 (Special operations in a File)

There are few important functions to be used with file streams like:

与文件流一起使用的重要功能很少,例如:

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

    tellp() -告知放置指针的当前位置。

    Syntax: filepointer.tellp()

    语法: filepointer.tellp()

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

    tellg() -告诉get指针的当前位置。

    Syntax: filepointer.tellg()

    语法: filepointer.tellg()

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

    seekp() -将放置指针移动到提到的位置。

    Syntax: filepointer.seekp(no of bytes,reference mode)

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

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

    seekg() -将get指针(输入)移动到指定位置。

    Syntax: filepointer.seekg((no of bytes,reference point)

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

  • put() - It writes a single character to file.

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

  • get() - It reads a single character from file.

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

Note: For seekp and seekg three reference points are passed:
ios::beg - beginning of the file
ios::cur - current position in the file
ios::end - end of the file
注意:对于seekp和seekg,传递了三个参考点:
ios :: beg-文件的开头
ios :: cur-文件中的当前位置
ios :: end-文件结尾

Below is a program to show importance of tellp, tellg, seekp and seekg:

下面是一个程序,以显示的重要性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;
}

New file created File Pointer Position is 13 As per tellp File Pointer Position is 12 As per tellg File Pointer Position is 5 As per tellg File Pointer Position is 6

创建的新文件文件指针位置为13每一个出纳文件文件指针位置为12每出入文件文件指针位置为5每出入文件文件指针位置为6

翻译自: https://www.studytonight.com/cpp/file-streams-in-cpp.php

c#处理文件流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值