實驗3 文件操做

一.實驗目的與要求:ios

  1. 學會使用文件操做函數實現對文件打開、關閉、讀、寫等操做。
  2. 學會對數據文件進行簡單的操做。
  3. 深刻理解 C++的輸入輸出的含義及其實現方法。
  4. 掌握標準輸入輸出流的應用。

二.實驗過程:編程

  1. 運行調試第8章編程示例8-2文本顯示程序;將其改寫爲一個隨機點名的程序,能夠參考如下步驟:

    (1)     讀入指定文本文件的程序,文本文件格式見參考內容;函數

    (2)     用隨機函數根據文本文件的記錄數量生成一個隨機數;oop

    (3)     根據這個隨機數,從所讀取的記錄中找到對應的記錄,並輸出顯示;ui

  1. 若還有時間,請嘗試運行調試第8章編程示例8.3-4;完成練習題8.4.1-3。

三.示例代碼:spa

  1. 編程示例8-2文本顯示程序:

 

#include <iostream>

#include <fstream>

using namespace std;

 

int main() {

    int c;   // input character

    int i;   // loop counter

    char filename[81];

    char input_line[81];

 

    cout << "Enter a file name and press ENTER: ";

    cin.getline(filename, 80);

 

    ifstream file_in(filename);

 

    if (! file_in) {

        cout << "File " << filename << " could not be opened." << endl;

        system("PAUSE");

        return -1;

    }

 

    while (1) {

        for (i = 1; i <= 24 && ! file_in.eof(); i++) {

            file_in.getline(input_line, 80);

            cout << input_line << endl;

        }

        if (file_in.eof())

            break;

        cout << "More? (Press 'Q' and ENTER to quit.)";

        cin.getline(input_line, 80);

        c = input_line[0];

        if (c == 'Q' || c == 'q')

            break;

    }

    system("PAUSE");

    return 0;

}

  

 

 

  1. 文本文件格式,從第二行起:

序號 學號             姓名調試

1    31140906012*  商**code

2    31150906010*  王*blog

3    31150906011*  韓*ci

 

  1. 編程示例8-3:
#include <iostream>

#include <fstream>

using namespace std;

 

int get_int(int default_value);

char name[20];

 

int main() {

    char filename[81];

    int n;

    int age;

    int recsize = sizeof(name) + sizeof(int);

   

 

    cout << "Enter file name: ";

    cin.getline(filename, 80);

 

    // Open file for binary read and write.   

 

    fstream  fbin(filename, ios::binary | ios::out);

    if (!fbin) {

        cout << "Could not open file " << filename << endl;

        system("PAUSE");

        return -1;

    }

 

//  Get record number to write to.

 

    cout << "Enter file record number: ";

    n = get_int(0);

 

    // Get data from end user.

 

    cout << "Enter name: ";

    cin.getline(name, 19);

    cout << "Enter age: ";

    age = get_int(0);

 

    // Write data to the file.

 

    fbin.seekp(n * recsize);

    fbin.write(name, 20);

    fbin.write(reinterpret_cast<char*>(&age), sizeof(int));

    fbin.close();

    system("PAUSE");

    return 0;

}

 

// Get integer function

// Get an integer from keyboard; return default

//  value if user enters 0-length string.

//

int get_int(int default_value) {

    char s[81];

 

    cin.getline(s, 80);

    if (strlen(s) == 0)

         return default_value;

    return atoi(s);

}

 

  1. 編程示例8-4:
#include <iostream>

#include <fstream>

using namespace std;

 

int get_int(int default_value);

char name[20];

 

int main() {

    char filename[81];

    int n;

    int age;

    int recsize =  sizeof(name) + sizeof(int);

   

 

    cout << "Enter file name: ";

    cin.getline(filename, 80);

 

    // Open file for binary read access.

 

    fstream  fbin(filename, ios::binary | ios::in);

    if (!fbin) {

        cout << "Could not open file " << filename << endl;

        system("PAUSE");

        return -1;

    }

 

while(1) {

 

        // Get record number and go to record.

 

        cout << "Enter file record number (-1 to quit): ";

        n = get_int(-1);

      if (n == -1)

          break;

       

      fbin.seekp(n * recsize);

 

        // Read data from the file.

 

        fbin.read(name, 20);

        fbin.read(reinterpret_cast<char*>(&age), sizeof(int));

 

        // Display the data and close.

   

        cout << "The name is: " << name << endl;

        cout << "The age is: " << age << endl;

    }

    fbin.close();

    system("PAUSE");

    return 0;

}

 

// Get integer function

// Get an integer from keyboard; return default

//  value if user enters 0-length string.

//

int get_int(int default_value) {

    char s[81];

 

    cin.getline(s, 80);

    if (strlen(s) == 0)

         return default_value;

    return atoi(s);

}
相關文章
相關標籤/搜索