惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
A
About on SuperTechFans
J
Java Code Geeks
量子位
博客园 - 三生石上(FineUI控件)
博客园 - Franky
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 慢步前行

实验0 安装GLUT包及工程的创建与运行 实验2 二维图形几何变换 实验1 时间趋势可视化 《鲜活的数据-第2章 处理数据》有关代码 本博客基本不再更新,请移步至我的CSDN博客 WebGL绘制三角形 WebGL画点程序v3 WebGL画点程序v2 WebGL画点程序v1 三步实现修改hosts方式登录谷歌 Maya API编程快速入门 我的高拍仪自动阅卷系统 实验8 标准模板库STL 实验7 多态与模板 实验6 继承 实验5 运算符重载 实验4 类初步 实验2 C++数组与指针 实验1 C++函数
实验3 文件操作
慢步前行 · 2017-03-17 · via 博客园 - 慢步前行

一.实验目的与要求:

  1. 学会使用文件操作函数实现对文件打开、关闭、读、写等操作。
  2. 学会对数据文件进行简单的操作。
  3. 深入理解 C++的输入输出的含义及其实现方法。
  4. 掌握标准输入输出流的应用。

二.实验过程:

  1. 运行调试第8章编程示例8-2文本显示程序;将其改写为一个随机点名的程序,可以参考以下步骤:

    (1)     读入指定文本文件的程序,文本文件格式见参考内容;

    (2)     用随机函数根据文本文件的记录数量生成一个随机数;

    (3)     根据这个随机数,从所读取的记录中找到对应的记录,并输出显示;

  1. 若还有时间,请尝试运行调试第8章编程示例8.3-4;完成练习题8.4.1-3。

三.示例代码:

  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*  商**

2    31150906010*  王*

3    31150906011*  韩*

  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);

}