2015级C++第16周程序阅读 文件操作-阿里云开发者社区

开发者社区> 开发与运维> 正文

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

简介: 1、请写出下面程序的输出结果 (1) #include <iostream> #include <fstream> using namespace std; int main() { fstream outfile,infile; outfile.open("data.dat",ios::out); outfile&lt

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

版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章