1.c++语言标准输入输出流

<1>控制符的用法

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int x=,y=,z=;
cout<<"Decimal:"<<x<<" "<<y<<" "<<z<<endl;//按十进制输出
cout<<"Octal:"<<oct<<x<<" "<<y<<" "<<z<<endl;//按八进制输出
cout<<"Hexademical:"<<hex<<x<<" "<<y<<" "<<z<<endl;//按十六进制输出
cout<<setiosflags(ios::uppercase);//设置数值中字母大写输出
cout<<"Hexademical:"<<hex<<x<<" "<<y<<" "<<z<<endl;//
cout<<resetiosflags(ios::uppercase);//设置数值中字母小写输出
cout<<"Hexademical:"<<hex<<x<<" "<<y<<" "<<z<<endl;//
cout<<"Decimal:"<<dec<<x<<" "<<y<<" "<<z<<endl;//恢复按十进制输出 return ;
}

  运行结果:

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int a=;
int b=;
cout<<setw()<<a<<endl;
cout<<setw()<<b;
return ;
}

setw操作符主要用来输出预留空格数,若空间多余则向右对齐;若空间不够,按数据长度输出。

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
cout<<setfill('*')//设置填充符号为"*"
<<setw()<<"OK"<<endl
<<setw()<<"OK"<<endl
<<setw()<<"OK"<<endl;
cout<<setfill(' ');//恢复默认设置,填充空格
return ;
}

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double test=22.0/;//c++语言默认的流输出数值有效位为六位
cout<<test<<endl;
cout<<setprecision()<<test<<endl//c++语言最小有效位为1位,此处取1
<<setprecision()<<test<<endl
<<setprecision()<<test<<endl
<<setprecision()<<test<<endl
<<setprecision()<<test<<endl;
cout<<setiosflags(ios::fixed);
cout<<setprecision()<<test<<endl;
//setiosflags(ios::fixed)与setprecision(8)合用,控制小数点右边的数字个数为八个
cout<<setprecision(); return ;
}

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x=,y=-8.246;
cout<<x<<" "<<y<<endl;
cout<<setiosflags(ios::showpoint);//设置强制显示小数点和无效0
cout<<x<<" "<<y<<endl; return ;
}

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x=,y=-8.246;
cout<<x<<" "<<y<<endl;
cout<<setiosflags(ios::showpos);//设置强制显示正号
cout<<x<<" "<<y<<endl; return ;
}

<2>.使用ios类成员函数

#include <iostream>
using namespace std;
int main(){
char str[];
cout<<"请输入一字符串:\n";
cin.getline(str,sizeof(str),',');
cout<<"输入的字符串为:"<<str<<endl; return ;
}

从键盘读取一行文本,每遇到一个逗号就结束一次输入。

   运行结果:

2.文件输入输出流

文件分为ASCII文件和二进制文件

文件的打开与关闭

ofstream、ifstream、fstream类

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ofstream myf("d:\\myabc.txt");
char txt[];
while ()
{
cin.getline(txt,);
if(strlen(txt)==)
break;
myf<<txt<<endl;
}
return ;
}

此程序可由用户输入任意一些字符串并按行保存到磁盘文件上。

#include <iostream.h>
#include <fstream.h>
#include <string.h>
int main(){
ifstream myf("d:\\myabc.txt",ios::nocreate);
if(myf.fail()){
cout<<"file not exist!"<<endl;
return ;
}
char txt[];
myf>>txt;
while(!myf.eof()){
cout<<txt<<endl;
myf>>txt;
}
return ;
}

该程序将上面文件内容输出到屏幕上

先定义文件流对象,再与文件连接

ifstream file;

file.open("myfile.txt",ios::in);//打开文件

file.close();//关闭文件

<1>文件的读写

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
ifstream ifile;
ofstream ofile;
ifile.open("d:\\myabc.txt");
ofile.open("d:\\213.txt");
if(ifile.fail()||ofile.fail()){
cerr<<"open file fail\n";
return EXIT_FAILURE;//EXIT_FAILURE在cstdlib库中定义
//用于向操作系统报告终止不成功
}
char ch;
ch=ifile.get();
while(!ifile.eof()){
ofile.put(ch);
ch=ifile.get();
}
ifile.close();
ofile.close();
return ;
}

文件复制

有时,程序用户需要交互输入数据文件名,则可使用下面的程序代码:

ifstream ifile;

string fileName;

cout<<"Enter the input file name:";

cin>>fileName;

ifile.open(fileName.c_str());

<2>文本文件的读写

要求:输入一个学生的姓名、学号、年龄和住址存入文本文件中,然后读出该文件的内容。

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
class student{
public:
char name[];
int num;
int age;
char addr[];
friend ostream & operator<<(ostream &out,student &s);
friend istream & operator>>(istream &in,student &s);
};
ostream & operator<<(ostream &out,student &s){
out<<s.name<<" "<<s.num<<" "<<s.age<<" "<<s.addr<<endl;
return out;
}
istream & operator>>(istream &in,student &s){
in>>s.name>>s.num>>s.age>>s.addr;
return in;
}
int main(){
ofstream ofile;
ifstream ifile;
ofile.open("d:\\s.txt");
student s;
for(int i=;i<=;i++){
cout<<"请输入第"<<i<<"个学生的姓名 学号 年龄 住址"<<endl;
cin>>s;
ofile<<s;
}
ofile.close();
cout<<"\n读出文件内容"<<endl;
ifile.open("d:\\s.txt");
ifile>>s;
while(!ifile.eof()){
cout<<s;
ifile>>s;
}
ifile.close(); return ;
}

<3>二进制文件的读写

文本文件和二进制文件最根本的区别在于进行I/O操作时对"\n"字符的解释方式不同。

通过随机数产生函数rand()产生20个整数,逐个将这些数以二进制方式写入文件file.dat中,然后读出这些数,在内存中对它们进行升序排序,再将排序后的数以文本方式逐个写入file.out文件中。

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
using namespace std;
void sort(int[],int);
int main(){
fstream dat,out; //定义文件流对象
int i,a[],b[];
dat.open("file.dat",ios::binary|ios::out|ios::in);//为读写打开二进制文件
if(!dat){
cout<<("cannot open file\n");
exit();
}
for(i=;i<;i++){
a[i]=rand();
dat.write((char*)&a[i],sizeof(int)); //将20个数读入文件
}
dat.seekg(); //将文件指针移至文件头
for(i=;i<;i++)
dat.read((char*)&b[i],sizeof(int)); //读出20个数
sort(b,); //调用排序函数
out.open("file.out",ios::out); //为输出打开文本文件
if(!out){
cout<<"cannot open file\n";
exit();
}
for(i=;i<;i++){
out<<b[i]<<" ";
}
out<<"\n";
for(i=;i<;i++){ //将排序后数据写入文本文件
cout<<setw()<<b[i];
if((i+)%==)
cout<<endl;
}
out.close(); //文本文件
dat.close();
return ; }
void sort(int x[],int m){ //排序函数
int i,j,k,t;
for(i=;i<m-;i++){
k=i;
for(j=i+;j<m;j++)
if(x[j]<x[k]) k=j;
if(k!=i){
t=x[i];x[i]=x[k];x[k]=t;
}
}
}

运行结果:

<4>文件的随机访问

#include <fstream>
#include <iostream>
using namespace std;
int main(){
fstream f("DATA",ios::in|ios::out|ios::binary);
int i;
for(i=;i<;i++)//先向文件中写20个二进制位表示的整数,无格式
f.write((char*)&i,sizeof(int));
streampos pos=f.tellp();//记录下当前的写指针
for(i=;i<;i++)//再向文件中写20个二进制位表示的整数,无格式
f.write((char*)&i,sizeof(int));
f.seekg(pos);//将读指针定位在pos所指的位置
f.read((char*)&i,sizeof(int));
cout<<"the data stored is"<<i<<endl;
f.seekp(,ios::beg);//移到文件开始
for(i=;i<;i++)//重写文件中的内容
f.write((char*)&i,sizeof(int));
f.seekg(pos);
f.read((char*)&i,sizeof(int));//将读指针定位在pos所指的位置
cout<<"the data stored is"<<i<<endl;
return ;
}

运行结果:

  

c++语言的输入输出流库的更多相关文章

  1. 标准C语言的输入输出流(i/o)方法详解

    cppreference.com -> 标准 C I/O ->详细说明 标准 C I/O clearerr 语法: #include <stdio.h> void cleare ...

  2. 【转载】标准C语言的输入输出流(i/o)方法详解

    标准 C I/O clearerr 语法: #include <stdio.h> void clearerr( FILE *stream ); clearerr函数重置错误标记和给出的流的 ...

  3. Java 输入输出流 转载

    转载自:http://blog.csdn.net/hguisu/article/details/7418161 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所 ...

  4. iostream/fstream中的输入输出流指针的绑定,tie函数的使用。

      为了兼容c语言的输入输出,c++里面采用tie将输入输出流经行绑定,所以cin/cout并不是独立的.当执行cin时,cout同时会被执行.反之亦然. by defalut,cin is tied ...

  5. java输入输出流总结 转载

    一.基本概念 1.1 什么是IO?     IO(Input/Output)是计算机输入/输出的接口.Java中I/O操作主要是指使用Java进行输入,输出操作.     Java所有的I/O机制都是 ...

  6. Java输入输出流(转载)

    转自http://blog.csdn.net/hguisu/article/details/7418161 目录(?)[+] 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作 ...

  7. Java 输入输出流 (七)

    1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...

  8. Java基础学习总结(47)——JAVA输入输出流再回忆

    一.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列. Java的I/O流提供了 ...

  9. Java中IO流,输入输出流概述与总结

    总结的很粗糙,以后时间富裕了好好修改一下. 1:Java语言定义了许多类专门负责各种方式的输入或者输出,这些类都被放在java.io包中.其中, 所有输入流类都是抽象类InputStream(字节输入 ...

随机推荐

  1. Django Model模型的实战操作笔记

    Model模型的实战操作笔记 1. 创建数据库和表 进入MySQL数据库创建数据库:mytest 进入数据库创建数据表:mytest_users CREATE TABLE `mytest_users` ...

  2. Knockout.js快速学习笔记

    原创纯手写快速学习笔记(对官方文档的二手理解),更推荐有时间的话读官方文档 框架简介(Knockout版本:3.4.1 ) Knockout(以下简称KO)是一个MVVM(Model-View-Vie ...

  3. 树上差分——点差分裸题 P3128 [USACO15DEC]最大流Max Flow

    讲解: https://rpdreamer.blog.luogu.org/ci-fen-and-shu-shang-ci-fen #include <bits/stdc++.h> #def ...

  4. john and hydra using de-ice1.100

    配置IP  ipconfig etho 192.168.179.111 http://192.168.179.111/index2.php curl http://192.168.179.111/in ...

  5. ESP-IDF版本2.1.1

    版本2.1.1是一个错误修复版本.它包括对KRACK和BlueBorne漏洞的修复. 版本2.1.1的文档可在http://esp-idf.readthedocs.io/en/v2.1.1/上找到. ...

  6. MySQL—函数大全

    一.数学函数: #ABS 绝对值函数 ) ; #BIN 返回二进制,OCT()八进制,hex十六进制 ); #ceiling 天花板整数,也就是大于x的整数 select CEILING(-13.5) ...

  7. Vuejs——(6)Vuejs与form元素

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   资料来于官方文档: http://cn.vuejs.org/guide/forms.html 本 ...

  8. CentOS No manual entry for man 没有 xx 的手册页条目

    yum install -y man man-pages man-pages-overrides https://unix.stackexchange.com/questions/182500/no- ...

  9. GET请求Referer限制绕过总结

    作者:Vulkey_Chen 原文来自:GET请求Referer限制绕过总结 前言 在做测试的时候会遇见这样几个漏洞场景: JSONP跨域劫持 反射XSS GET请求类型攻击 但是,在相对安全的情况下 ...

  10. C#6.0语言规范(二) 词法结构

    程式 AC#程序由一个或多个源文件组成,正式称为编译单元(编译单元).源文件是Unicode字符的有序序列.源文件通常与文件系统中的文件一一对应,但不需要此对应关系.为了获得最大的可移植性,建议使用U ...