2015级C++第16周程序阅读 文件操作

1、请写出下面程序的输出结果
(1)

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    fstream outfile,infile;
    outfile.open("data.dat",ios::out);
    outfile<<"1111111111"<<endl;
    outfile<<"aaaaaaaaaa"<<endl;
    outfile<<"AAAAAAAAAA"<<endl;
    outfile<<"**********"<<endl;
    outfile.close();
    infile.open("data.dat",ios::in);
    char line[80];
    int i=0;
    while(!infile.eof())
    {
        i++;
        infile.getline(line,sizeof(line));
        cout<<i<<": "<<line<<endl;
    }
    infile.close();
    return 0;
}

(2)说出程序的功能,并上机验证(请自建a.txt)

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream readFile;
    ofstream writeFile;
    char ch;
    readFile.open("a.txt", ios::in);
    writeFile.open("b.txt", ios::out);
    while (readFile.get(ch))
        writeFile.put(ch);
    readFile.close();
    writeFile.close();
    cout << "Finish!" << endl;
    return 0;
}

(3)

#include <iostream>
#include <fstream>
#include<cstring>
using namespace std;
int main()
{
    ifstream readFile;
    ofstream writeFile;
    char ch[100];
    readFile.open("a.txt", ios::in);
    writeFile.open("b.txt", ios::out);
    while (!readFile.eof())
    {
        readFile.getline(ch,100,'\n');
        writeFile.write(ch,strlen(ch));
        writeFile.write("\n",1);
    }
    readFile.close();
    writeFile.close();
    cout << "Finish!" << endl;
    return 0;
}

2、阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer)。查看其内容,并理解二进制文件存储的原理。
(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;
}

3、查看下面程序的输出,解释为什么会有这样的输出。

#include <iostream>
#include <fstream>
using namespace std;
int main( )
{
    unsigned char a[] = {0xD4,0xB8,0xB7,0xAD,0xD7,0xAA,0xB5,0xC4,0x43,0x2B,
                         0x2B,0xBF,0xCE,0xCC,0xC3,0xCE,0xAA,0xC4,0xE3,0xB4,
                         0xF8,0xC0,0xB4,0xD1,0xA7,0xCF,0xB0,0xB7,0xBD,0xB7,
                         0xA8,0xB5,0xC4,0xB8,0xC4,0xB1,0xE4,0xA3,0xA1
                        };
    ofstream outfile("f3.dat",ios::out|ios::binary);
    outfile.write((char*)a, sizeof(a));
    outfile.close();
    return 0;
}

4、阅读下面的程序,指出其功能,体会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.seekg (0, ios::end);
    m = file.tellg();
    file.close();
    cout << "size of " << filename;
    cout << " is " << (m-l) << " bytes.\n";
    return 0;
}

(2)

#include <fstream>
using namespace std;
int main ()
{
    long pos;
    ofstream outfile;
    outfile.open ("test.txt");
    outfile.write ("This is an apple",16);
    pos=outfile.tellp();
    outfile.seekp (pos-7);
    outfile.write (" sam",4);
    outfile.close();
    return 0;
}

(3)

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    fstream outfile,infile;
    outfile.open("data.txt",ios::out);
    for (int i=0; i<26; i++)
        outfile<<(char)('A'+i);
    outfile.close();
    infile.open("data.txt",ios::in);
    char ch;
    infile.seekg(6,ios::beg);
    if(infile.get(ch))
        cout<<ch;
    infile.seekg(8,ios::beg);
    if(infile.get(ch))
        cout<<ch;
    infile.seekg(-8,ios::end);
    if(infile.get(ch))
        cout<<ch;
    cout<<endl;
    infile.close();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值