(1)问题及代码:
/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
* 文件名称:Project4.cpp
* 作 者:陈旭
* 完成日期:2015年6月15日
* 版 本 号:v1.0
*
* 问题描述:阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法)。
查看其内容,并理解文件存储的原理。
* 输入描述:略
* 程序输出:略
*/
#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)问题及代码:
/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
* 文件名称:Project4.cpp
* 作 者:陈旭
* 完成日期:2015年6月15日
* 版 本 号:v1.0
*
* 问题描述:阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法)。
查看其内容,并理解文件存储的原理。
* 输入描述:略
* 程序输出:略
*/
#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)问题及代码:
/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
* 文件名称:Project4.cpp
* 作 者:陈旭
* 完成日期:2015年6月15日
* 版 本 号:v1.0
*
* 问题描述:阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法)。
查看其内容,并理解文件存储的原理。
* 输入描述:略
* 程序输出:略
*/
#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;
}
运行结果: