首页 > 其他分享 >实验6 模板类和文件I/O

实验6 模板类和文件I/O

时间:2022-12-02 14:34:35浏览次数:33  
标签:std 文件 cout int size 实验 include 模板 out

task3_1.cpp

#include <iostream>
#include <fstream>
#include <array>
#define N 5

int main() {
    using namespace std;

    array<int, N> x {97, 98, 99, 100, 101};

    ofstream out;
    out.open("data1.dat", ios::binary);
    if(!out.is_open()) {
        cout << "fail to open data1.dat\n";
        return 1;
    }

    out.write(reinterpret_cast<char *>(&x), sizeof(x));
    out.close();
}

task3_2.cpp

#include <iostream>
#include <fstream>
#include <array>
#define N 5

int main() {
    using namespace std;
    array<int, N> x;

    ifstream in;
    in.open("data1.dat", ios::binary);
    if(!in.is_open()) {
        cout << "fail to open data1.dat\n";
        return 1;
    }

    in.read(reinterpret_cast<char *>(&x), sizeof(x));
    in.close();

    for(auto i = 0; i < N; ++i)
        cout << x[i] << ", ";
    cout << "\b\b \n";
}

 

问题:存储时用int类型,占四个字节,读取时按char一个字节读,97的四个字节赋值给char变量,隐式转换为a,后面的三个字节为空,接着赋值97,转换为b

task4.hpp

#pragma once
#include <iostream>
using namespace std;

template <typename T>
class Vector
{
public:
    Vector(int n)
    {
        this->size = n;
        v = new T[n];
    }
    Vector(int n, T t)
    {
        this->size = n;
        v = new T[n];
        for (int i = 0; i < n; i++)
        {
            this->v[i] = t;
        }
    }
    T &at(int n)
    {
        return this->v[n];
    }
    T &operator[](int n)
    {
        return this->v[n];
    }
    int getsize()
    {
        return this->size;
    }

private:
    int size;
    T *v;
};
template <typename T>
void output(Vector<T> &v)
{
    for (int i = 0; i < v.getsize(); i++)
        cout << v[i] << ' ';
    puts("");
}

task5.cpp

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void output(std::ostream &out)
{
    char cipher[100][100];
    cipher[0][0] = ' ';
    for (int i = 1; i <= 26; i++)
        cipher[0][i] = 'a' + i - 1;
    for (int i = 1; i <= 26; i++)
    {
        cipher[i][0] = '0' + i;
        for (int j = 1; j <= 26 - i; j++)
        {
            cipher[i][j] = 'A' + i + j - 1;
        }
        for (int k = 26 - i + 1, m = 0; k <= 26; k++, m++)
        {
            cipher[i][k] = 'A' + m;
        }
    }
    for (int i = 0; i <= 26; i++)
    {
        if (i != 0)
            cout << setw(2) << cipher[i][0] - '0';
        else
            cout << setw(2) << cipher[i][0];
        for (int j = 1; j <= 26; j++)
            cout << setw(2) << cipher[i][j];
        puts("");
    }
    for (int i = 0; i <= 26; i++)
    {
        if (i != 0)
            out << setw(2) << cipher[i][0] - '0';
        else
            out << setw(2) << cipher[i][0];
        for (int j = 1; j <= 26; j++)
            out << setw(2) << cipher[i][j];
        out << '\n';
    }
}
int main()
{
    ofstream out;
    out.open("cipher_key.txt", ios::out);
    if (!out.is_open())
    {
        cout << "fail!!" << endl;
        return 1;
    }
    output(out);
    return 0;
}

cipher_key.txt

 

标签:std,文件,cout,int,size,实验,include,模板,out
From: https://www.cnblogs.com/cytxxx/p/16940969.html

相关文章

  • Linux服务器无法删除文件怎么办?如何处理?
    当我们使用Linux服务器的时候,很多人应该都遇到过这种情况:有些文件的属性被修改,从而导致我们无法删除文件,使用root用户也不行,那么Linux服务器无法删除文件如何处理?小编......
  • windows 无法验证创建此文件的人员 的解决方法
    在win7系统中打开程序的时候出现了:【打开文件-安全警告】错误弹窗:我们无法验证创建此文件的人员,是否确定要与运行此文件?的错误提示,该怎么办呢?当我们在win10系统中每次打开......
  • PLSQL导入/导出数据方法--dmp文件
    转:PLSQL导入/导出数据方法以前导数据库信息的时候,总是会先开启sql窗口,把自己手写的建表文件复制进去,然后再导入数据信息。今天突然懒得去找以前的建表文件,而想用SLSQL直......
  • 使用DocXToPdfConverter把Docx转成Pdf文件
    1、安装DocXToPdfConverterInstall-PackageDocXToPdfConverter2、下载LibreOfficehttps://www.libreoffice.org/download/portable-versions/下载后安装软件。3、......
  • .net文件分片上传,断点续传
    ​IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头。 一. 两个必要响应头Accept-Ranges、ETag      ......
  • 实验六
    task3_1.cpp:#include<iostream>#include<fstream>#include<array>#defineN5intmain(){ usingnamespacestd; array<int,N>x{97,98,99,100,101};......
  • Python怎样写个通用爬虫模板?
    其实经常写爬虫的程序员应该都知道,做一个爬虫工作无非就是三个步骤:下载数据、解析数据、保存数据。基本所有爬虫万变不离其宗,都是这样的套路。下面就是Python写出来一个完......
  • Mysql的安装和卸载(删除注册表的残余文件)
    对于会忘记数据库的密码,或者改了数据库密码后无法登录,可以把数据库先卸载在安装,不过原来建的数据可能丢失,因此在平时练习等操作的时候要有备份的好习惯。我看网上有很多说......
  • application.yml与application.properties文件的相互转换、属性提取
    packagecom.liftsail.testprofiledemo.utiltest;importorg.springframework.util.CollectionUtils;importjava.util.*;/***@Author:liftsail*@Date:2022/......
  • Linux系统各文件目录的作用
    1./bin/:存放系统命令的目录,普通用户和超级用户都可执行,单用户下可执行2./sbin/:保存和系统环境设置相关的命令,只有超级用户可执行,但有些允许普通用户查看3./usr/bin/:存......