I/O的概念和流类库的结构
程序的输入指的是从输入文件将数据传送给程序,程序的输出指的是从程序将数据传送给文件
C++输入输出包含以下三个方面的内容:
对系统指定的标准设备的输入和输出.就是从键盘输入数据,输入到显示器屏幕.这种输入称为标准的输入输出,简称标准I/O.
以外存磁盘文件为对象进行输入和输出,即从磁盘文件输入数据,数据输出到磁盘文件.以外存文件为对象的输入输出称为文件的输入输出,简称文件I/O.
对内存中指定的空间进行输入和输出.通常指定一个字符数组作为存储空间(实际上可以利用该空间存储任何信息).这种输入输出称为字符串输入输出,简称I/O串.
c++的I/O对c的发展----类型安全和可扩展性.
在C语言中,用printf 和scanf进行输入输出,往往不能保证所输入输出的数据时可靠安全的.在c++的输入输出中,编译系统对数据类型进行了严格的检查,凡是类型不正确的数据都不能通过编译.因此c++的I/O操作是安全的.C++的I/O操作是可以扩展的,不仅可以用来输入输出标准类型的数据,也可以用于用户自定义类型的数据.
C++通过I/O类库实现丰富的I/O功能,这样使C++的输入输出明显的优于C语言中的printf 和scanf,但是也为之付出了代价,C++的I/O系统变得比较复杂,要掌握许多细节
C++编译系统提供了用于输入输出的iostream类库,iostream这个单词是由三个部分组成的
格式化I/O
例子
#include
#include
//格式化I/O
using namespace std;
int main (void) {
//格式化的写
ofstream ofs ("format.txt"); //相当与c中的w,新加的内容会覆盖原有内容,打开这个文件,打开失败,ofs为false,成功为true
if (! ofs) {
perror ("打开文件失败");
return -1;
}
ofs << 1234 << ' ' << 56.78 << ' ' << "tarena"
<< '
'; //将这写写入到文件里
ofs.close (); //关闭文件,如果不写也可以,结束后,调用ofstream析构函数会关闭掉
ofs.open ("format.txt", ios::app); //相当于c中的以a方式打开,可以在文件中追加,不会覆盖原有的内容
if (! ofs) {
perror ("打开文件失败");
return -1;
}
ofs << "append_a_line
";
ofs.close ();
//格式化的读
ifstream ifs ("format.txt"); //要求文件必须存在,否则报错
if (! ifs) {
perror ("打开文件失败");
return -1;
}
int i;
double d;
string s1, s2;
ifs >> i >> d >> s1 >> s2; //读取文件中的内容到程序中。
cout << i << ' ' << d << ' ' << s1 << ' '
<< s2 << endl;
ifs.close (); //关闭文件
return 0;
}
非格式化I/O
例子
#include
#include
//非格式化I/O
using namespace std;
int main (void) {
ofstream ofs ("putget.txt"); //定义一个ofstream类
if (! ofs) {
perror ("打开文件失败");
return -1;
}
for (char c = ' '; c <= '~'; ++c) //前加加返回的是一个引用,效率高,后加加返回的是拷贝,要进行一次拷贝,所以效率低。
if (! ofs.put (c)) { //ofs.put成功返回true,否则false。向文件中写入。
perror ("写入文件失败");
return -1;
}
ofs.close ();
ifstream ifs ("putget.txt");
if (! ifs) {
perror ("打开文件失败");
return -1;
}
char c;
while ((c = ifs.get ()) != EOF) //读如字符,直到返回EOF(表示读取完毕)。
cout << c;
cout << endl;
if (! ifs.eof ()) { //或者if(ifs.error())都可以判断是否出错
perror ("读取文件失败");
return -1;
}
ifs.close ();
return 0;
}
随机I/O
例子
#include
#include
//随机I/O
using namespace std;
int main (void) {
fstream fs ("seek.txt", ios::in | ios::out); //即可读也可写,相当于c中的r+,要求文件必须存在。
if (! fs) {
perror ("打开文件失败"); //打印错误信息。最近的一次错误的原因。
return -1;
}
fs << "0123456789"; //向文件里输入。
cout << fs.tellp () << endl; //获取写指针的位置,在下一个接受数据的位置,最后一个9所在的位置是9,所以写的位置为10
cout << fs.tellg () << endl; //获取读指针的位置,虽然没有读,但是会随着写指针一起走
//seekp()与seekg()函数分别有两个参数,第一个是偏移量,正负代表前后方向,第二个是从哪个位置开始
fs.seekp (-3, ios::cur); //调整写指针的位置,表示从当前位置往文件头移动三个字符
fs << "XYZ"; //覆盖789
fs.seekg (4, ios::beg); //调整读指针的位置,表示从文件头开始偏移四个位置
int i;
fs >> i; //从4开始读,读到6,后面是xyz所以不读,结束。
cout << i << endl;
cout << fs.tellg () << endl; //7
cout << fs.tellp () << endl; //7
fs.seekg (-6, ios::end); //从文件尾开始向文件头偏移6个位置
fs << "ABC";
fs.close ();
return 0;
}
二进制I/O
例子:加密文件(异或机制)
#include
#include
#include
#include
using namespace std;
#define BUFSIZE (1024*10)
int _xor (const char* src, const char* dst,
unsigned char key) { //源文件,目标文件,密钥
ifstream ifs (src, ios::binary); //以二进制方式读
if (! ifs) {
perror ("打开源文件失败");
return -1;
}
ofstream ofs (dst, ios::binary);
if (! ofs) {
perror ("打开目标文件失败");
return -1;
}
char* buf = NULL; //创建缓冲区
try {
buf = new char[BUFSIZE];
}
catch (bad_alloc& ex) { //bad_alloc是标准库里的
cout << ex.what () << endl;
return -1;
}
while (ifs.read (buf, BUFSIZE)) { //缓冲区的地址,和大小
for (size_t i = 0; i < BUFSIZE; ++i)
buf[i] ^= key; //将每一个字符都与key异或
if (! ofs.write (buf, BUFSIZE)) { //以二进制方式写进去,缓冲区地址,和希望写的大小
perror ("写入文件失败");
return -1;
}
}
if (! ifs.eof ()) { //判断是否正常
perror ("读取文件失败");
return -1;
}
for (size_t i = 0; i < ifs.gcount (); ++i) //gcount函数返回剩下的大小
buf[i] ^= key; //将缓冲区中的剩下的也与key异或
if (! ofs.write (buf, ifs.gcount ())) {
perror ("写入文件失败");
return -1;
}
delete[] buf; //释放缓冲区
ofs.close (); //关闭文件
ifs.close (); //关闭文件
return 0;
}
int enc (const char* plain, const char* cipher) {
srand (time (NULL));
unsigned char key = rand () % 256; //0到256sui随机数
if (_xor (plain, cipher, key) == -1) //为-1则失败
return -1;
cout << "密钥:" << (unsigned int)key << endl; //告诉密钥是什么。转换为数的形式
return 0;
}
int dec (const char* cipher, const char* plain,
unsigned char key) {
return _xor (cipher, plain, key);
}
int main (int argc, char* argv[]) {
if (argc < 3) {
cerr << "用法:" << argv[0]
<< " <明文文件> <密文文件>" << endl;
cerr << "用法:" << argv[0]
<< " <密文文件> <明文文件> <密钥>"
<< endl;
return -1;
}
if (argc < 4)
return enc (argv[1], argv[2]);
else
return dec (argv[1], argv[2],
atoi (argv[3])); return 0; }
页面更新:2024-05-15
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号