日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C/C++的文件IO函數你知道嗎_C 語言

作者:蔡高數 ? 更新時間: 2022-05-02 編程語言

文件(file)通常是在磁盤或固態硬盤上的一段已命名的存儲區。C中采用的主要是文件指針的辦法,C++中對文件的操作主要運用了“文件流”(即非標準的輸入輸出)的思想。

一、C

C把文件看作是一系列連續的字節,每個字節都能被單獨讀取。C提供兩種文件模式:文本模式和二進制模式。

1.fopen函數

std::FILE* fopen( const char* filename, const char* mode );

打開文件filename并返回與該文件關聯的文件流。mode用于確定文件訪問模式。

參數?

filename - 將文件流關聯到的文件名
mode - 以空字符結尾的字符串確定文件訪問模式

返回值

如果成功,則返回一個指向控制已打開文件流的對象的指針,同時清除 eof 和錯誤位。出錯時,返回一個空指針。

2.fclose

int fclose(FILE *stream)

C 庫函數?int fclose(FILE *stream)?關閉流 stream。刷新所有的緩沖區。

參數

stream?是指向 FILE 對象的指針,該 FILE 對象指定了要被關閉的流。

返回值

如果流成功關閉,則該方法返回零。如果失敗,則返回 EOF。

3.FILE結構體

C語言的stdio.h頭文件中定義了用于文件操作的結構體FILE。通過fopen返回一個文件指針(指向FILE結構體的指針)來進行文件操作。可以在stdio.h頭文件中查看FILE結構體的定義,如下:

?不過在visual studio中貌似被隱藏了,見https://docs.microsoft.com/en-us/cpp/porting/visual-cpp-change-history-2003-2015

4.fprintf()和fscanf()函數

文件I/O函數 fprintf() 和 fscanf() 函數的工作方式與 printf() 和 scanf()類 似,區別在于前者需要用第1個參數指定待處理的文件。

int fprintf(FILE *stream, const char *format, ... );
fprintf()函數根據指定的format(格式)發送信息(參數)到由stream(流)指定的文件.
int fscanf(FILE *stream, const char *format, ...)
C庫函數 int fscanf(FILE *stream, const char *format, ...) 從流stream讀取格式化輸入。
  • stream?-- 指向 FILE 對象的指針,該 FILE 對象標識了流。
  • format?-- C 字符串,包含了以下各項中的一個或多個:空格字符、非空格字符?和?format 說明符。

成功則返回成功賦值或寫入的個數,失敗或到達文件末尾返回負數。

二、C++

頭文件fstream 定義了三個類型來支持文件IO: ifstream從一個給定文件讀取數據,ofstream向一個給定文件寫入數據,以及fstream可以讀寫給定文件。

每個流都有一個關聯的文件模式(file mode),用來指出如何使用文件。表8.4列出了文件模式和它們的含義。

每個文件流類型都定義了一個默認的文件模式,當未指定文件模式時就使用此默認模式。與ifstream關聯的文件默認以in模式打開,與ofstream關聯的文件默認以out模式打開,與fstream關聯的文件默認以in和out模式打開。

默認情況下,打開一個ofstream時,文件的內容會被丟棄。阻止一個ofstream清空給定文件內容的方法是同時指定app或in模式:

//在這幾條語句中,filel都被截斷
ofstream out ("filel"); //隱含以輸出模式打開文件并截斷文件
ofstream out2("file1", ofstream::out); //隱含地截斷文件
ofstream out3("file1", ofstream::out | ofstream::trunc);
//為了保留文件內容,顯式指定app模式
ofstream app("file2", ofstream::app); //隱含為輸出模式
ofstream app2("fi1e2", ofstream::out | ofstream::app);

三、示例程序

#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX_NUM = 100;
int a[MAX_NUM];
int n = 2;
int main(int argc, char* argv[])
{
    FILE* file4 = fopen("d.txt", "w");
    if (!file4) {
        printf("file4 open error!\n");
        return -1;
    }
    //fprintf(file4, "name age sex position\n");
    int age, sex;
    char name[50], position[50];
    printf("please input 2 date : name age sex position:\n");
    while (n--)
    {
        scanf("%s %d %d %s", &name, &age, &sex, &position);
        fprintf(file4, "%s %d %d %s\n", name, age, sex, position);
    }
    fclose(file4);
    FILE* file5 = fopen("d.txt", "r");
    if (!file5) {
        printf("file5 open error!");
        return -1;
    }
    int m = 2, ans, ans1;
    while (m--)
    {
        fscanf(file5, "%s %d %d %s", &name, &age, &sex, &position);
        printf("%s %d %d %s\n", name, age, sex, position);
    }
    fclose(file5);
    fstream file1("d.txt");
    if (!file1) {
        cout << "file1 open error! " << endl;
        return -1;
    }
    string tmp;
    vectorstr;
    while (file1 >> tmp)
    {
        str.push_back(tmp);
        cout << tmp << endl;
    }
    ifstream file2("b.txt", ios::in);
    if (!file2) {
        cout << "file2 open error! " << endl;
        return -1;
    }
    ofstream file3("c.txt", ios::out);
    if (!file3) {
        cout << "file3 open error! " << endl;
        return -1;
    }
    while (n--)
    {
        file3 << n<<' ';
    }
    file1.close();
    file2.close();
    file3.close();
    return 0;
}

輸入輸出結果如下:

總結

原文鏈接:https://blog.csdn.net/qq_44272681/article/details/122712161

欄目分類
最近更新