文件学习

 

#include <fstream>
#include <iostream>
using namespace std;

bool more(ifstream& fin, int& n)
{
    if (fin >> n)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool copy(ofstream& fout, ifstream& fin, int& n)
{
    fout << " " << n;
    return more(fin, n);
}

int main()
{
    ifstream fin1("north.txt");
    ifstream fin2("south.txt");
    ofstream fout("combined.txt");
    ofstream fout2("adress.txt");
    cout << "&fin1 " << &fin1 << endl;
    fout2 << "&fin1 " << &fin1 << endl;
    cout << "&fin2 " << &fin2 << endl;
    fout2 << "&fin2 " << &fin2 << endl;
    cout << "&fout " << &fout << endl;
    fout2 << "&fout " << &fout << endl;
    cout << "&fout2 " << &fout2 << endl;
    fout2 << "&fout2 " << &fout2 << endl;   
    int n1, n2;
    bool more1 = more(fin1, n1);
    bool more2 = more(fin2, n2);
    cout << "fin1 " << "0x22fd60 - 0x22fdd4 = " << 0x22fd60 - 0x22fdd4 << endl;
    fout2 << "fin1 " << "0x22fd60 - 0x22fdd4 = " << 0x22fd60 - 0x22fdd4 << endl;
    cout << "fin2 " << "0x22fe60 - 0x22fed4 = " << 0x22fe60 - 0x22fed4 << endl;
    fout2 << "fin2 " << "0x22fe60 - 0x22fed4 = " << 0x22fe60 - 0x22fed4 << endl;
    cout << "sizeof(fin1) = " << sizeof(fin1) << endl;
    fout2 << "sizeof(fin1) = " << sizeof(fin1) << endl;
    cout << "sizeof(fin2) = " << sizeof(fin2) << endl;
    fout2 << "sizeof(fin2) = " << sizeof(fin2) << endl;
    cout << "sizeof(ifstream) = " << sizeof(ifstream) << endl;
    fout2 << "sizeof(ifstream) = " << sizeof(ifstream) << endl;
    cout << "fin1 north: 13 * sizeof(int) = " << 13 * sizeof(int) << endl;
    fout2 << "fin1 north: 13 * sizeof(int) = " << 13 * sizeof(int) << endl;
    cout << "fin2 south: 9 * sizeof(int) = " << 9 * sizeof(int) << endl;
    fout2 << "fin2 south: 9 * sizeof(int) = " << 9 * sizeof(int) << endl;
    while (more1 && more2)
    {
        if (n1 < n2)
        {
            cout  << n1 << "\t";
            cout << "fin1 " << &fin1;
            fout2 << "n1 = " << n1;
            fout2 << " " << "fin1 " << fin1 << " " << &fin1 << endl;
            more1 = copy(fout, fin1, n1);           
        }
        else
        {
            cout << n2 << "\t";
            cout << "fin2 " << &fin2;
            fout2 << "n2 = " << n2;
            fout2 << " " << "fin2 " << fin2 << " " << &fin2 << endl;
            more2 = copy(fout, fin2, n2);           
        }       
    }
    while(more1)
    {
        more1 = copy(fout, fin1, n1);
        cout  << n1 << "\t";
        cout << "fin1 " << &fin1;
        fout2 << "n1 = " << n1;
        fout2 << " " << "fin1 " << fin1 << " " << &fin1 << endl;
    }
    while(more2)
    {
        more2 = copy(fout, fin2, n2);
        cout << n2 << "\t";
        cout << "fin2 " << &fin2;
        fout2 << "n2 = " << n2;
        fout2 << " " << "fin2 " << fin2 << " " << &fin2 << endl;
    }
    fout << endl;
    cout << endl;       
    system("pause");
    return 0;
}

north: 22 25 40 44 48 52 55 70 75 77 80 88 99

south: 20 30 33 47 50 60 66 72 85

combined:  20 22 25 30 33 40 44 47 48 50 52 55 60 66 70 72 75 77 80 85 88 99
address:

&fin1 0x22fe60
&fin2 0x22fd60
&fout 0x22fc60
&fout2 0x22fb60
fin1 0x22fd60 - 0x22fdd4 = -116
fin2 0x22fe60 - 0x22fed4 = -116
sizeof(fin1) = 252
sizeof(fin2) = 252
sizeof(ifstream) = 252
fin1 north: 13 * sizeof(int) = 52
fin2 south: 9 * sizeof(int) = 36
n2 = 20 fin2 0x22fdd4 0x22fd60
n1 = 22 fin1 0x22fed4 0x22fe60
n1 = 25 fin1 0x22fed4 0x22fe60
n2 = 30 fin2 0x22fdd4 0x22fd60
n2 = 33 fin2 0x22fdd4 0x22fd60
n1 = 40 fin1 0x22fed4 0x22fe60
n1 = 44 fin1 0x22fed4 0x22fe60
n2 = 47 fin2 0x22fdd4 0x22fd60
n1 = 48 fin1 0x22fed4 0x22fe60
n2 = 50 fin2 0x22fdd4 0x22fd60
n1 = 52 fin1 0x22fed4 0x22fe60
n1 = 55 fin1 0x22fed4 0x22fe60
n2 = 60 fin2 0x22fdd4 0x22fd60
n2 = 66 fin2 0x22fdd4 0x22fd60
n1 = 70 fin1 0x22fed4 0x22fe60
n2 = 72 fin2 0x22fdd4 0x22fd60
n1 = 75 fin1 0x22fed4 0x22fe60
n1 = 77 fin1 0x22fed4 0x22fe60
n1 = 80 fin1 0x22fed4 0x22fe60
n2 = 85 fin2 0x22fdd4 0x22fd60
n1 = 99 fin1 0x22fed4 0x22fe60
n1 = 99 fin1 0 0x22fe60

n1 最后一个为0,是因为fin1取数据失败了,返回的空指针。

注意:fin1、fin2指向的是文件的首地址指针。

有关文件的内存分配,跟变量的分配原则是不一样的。这个需要去学习计算机操作系统,怎么读取文件,并且给它分配内存的。

n1是一个变量。文件在另外一个地方。所以指针的操作是从文件内存区域,取数据复制给n1变量空间。

 

最后,不是总结的总结:了解内存结构组织和使用方法,会有解惑的感觉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值