第15周 程序阅读-二进制文件及文件的读取1

1、阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法)。查看其内容,并理解文件存储的原理。

(1)

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main( )
{
    int a;
    ofstream outfile("f1.dat",ios::out);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    cin>>a;
    outfile<<a<<endl;
    outfile.close();
    return 0;
}

(2)

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main( )
{
    int a;
    ofstream outfile("f2.dat",ios::out|ios::binary);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    cin>>a;
    outfile.write((char*)&a, sizeof(int));
    outfile.close();
    return 0;
}
时间: 2024-08-08 13:43:22

第15周 程序阅读-二进制文件及文件的读取1的相关文章

第15周 程序阅读-二进制文件及文件的读取4

4.阅读并运行下面的示例,体会二进制文件和字符串流操作的一般方法. 例17 #include <strstream> #include<iostream> using namespace std; struct student {     int num;     char name[20];     float score; }; int main( ) {     student stud[3]={1001,"Li",78,1002,"Wang&q

15周《C++语言基础》程序阅读——二进制文件及文件的随机读写(1)

1.阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法).查看其内容,并理解文件存储的原理. (1) <code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Cod

第15周 程序阅读-二进制及二进制文件的读取3

3.阅读下面的程序,指出其功能,体会seekg().tellg()等函数的功能及其用法 (1) #include<iostream> #include <fstream> using namespace std; const char * filename = "a.txt"; int main () { long l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file

第15周 程序阅读-二进制及二进制文件的读取2

2.查看下面程序的输出,解释为什么会有这样的输出. #include <iostream> #include <fstream> using namespace std; int main( ) { unsigned char a[] = {0x32,0x30,0x31,0x35,0xA3,0xAC,0xCE,0xD2,0xC3,0xC7,0xB3,0xD4, 0xC1,0xCB,0xB5,0xDA,0xD2,0xBB,0xBF,0xDA,0xF3,0xA6,0xD0,0xB7};

第15周 二进制文件及文件的读取5

例18 #include <strstream> #include<iostream> using namespace std; int main( ) { char c[50]="12 34 65 -23 -32 33 61 99 321 32"; int a[10],i,j,t; cout<<"array c:"<<c<<endl;//显示字符数组中的字符串 istrstream strin(c,siz

第九周-程序阅读理解

/*Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:my.cpp *作 者:张瀚文 *完成日期:2016年5月6日 * *问题描述: 阅读程序,写出程序的运行结果并理解其运行机制. */ #include <iostream> #include<cstring> using namespace std; class A { char *a; public: A(char *aa) { a=new char[s

第九周-程序阅读

/*Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:my.cpp *作 者:张瀚文 *完成日期:2016年5月6日 * *问题描述: 阅读程序,写出程序的运行结果并理解其运行机制. */ #include <iostream> #include<cstring> using namespace std; class A { char *a; public: A(A &t); A(char *aa) {

第二周 程序阅读

#include <iostream> #include <cstring> using namespace std; class Student { private: int num; char name[20]; char sex; public: void set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void display( ) { cout<<"num: &quo

第九周 程序阅读-学生信息管理系统

阅读程序"简单C++学生信息管理系统",找出其中出现构造函数.友元函数.运算符重载.静态数成员语法现象出现的位置,仔细体会其用法,在以后的设计中能够灵活应用有关方法和技巧. #include <iostream> #include <cstring> using namespace std; #define MAX 100 class CDate // 定义日期类 { private: unsigned short int year; // 年 unsigned