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. solr7.7.0搜索引擎使用(二)(添加搜索)

    一.安装完毕之后,需要为solr添加core,每一个搜索server就是一个core,solr可以有很多core,我们需要创建一个core用于我们的搜索 添加core的方式有两种: 第一种进入solr ...

  2. 【转】comparable Interface

    作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 , ...

  3. Docker 启动不了容器的问题

    今天在运行 docker 的时候,就是执行 docker exec 命令的时候,发现一直报错.具体的报错信息如下: Error response from daemon: Container XXX ...

  4. Unity3d让某个物体一直正对着相机

    //将以下代码绑定到相机上 using UnityEngine; using System.Collections;   public class LookatScipt : MonoBehaviou ...

  5. 大数据搜索引擎之elasticsearch使用篇(一)

    作者:yanzm 原文来自:https://bbs.ichunqiu.com/thread-42421-1-1.html 1.基础介绍 本期,我们将着重介绍elasticsearch的基本使用方法. ...

  6. The MAC is invalid

    在使用laravel框架进行网站开发时,我们会使用laravel的Crypt类对用户的密码进行加密来达到信息加密的目的,Crypt类会对数据加密时会依赖APP_KEY,所以当更换了APP_KEY时,再 ...

  7. Windows 系统里面的 hosts 文件

    一.什么是hosts文件? hosts文件是一个用于储存计算机网络中各节点信息的计算机文件.这个文件负责将主机名映射到相应的IP地址.hosts文件通常用于补充或取代网络中DNS的功能.和DNS不同的 ...

  8. Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!

    前几天写了一篇 Java 8 即将在 2019 年停止免费向企业提供更新的文章,企图迫使用户向更新一代的 Java 版本升级,但让人遗憾的是,小编今天收到了 Oracle Java 版本的升级推送,装 ...

  9. [视频]K8飞刀 正则采集WordPress站点用户

    链接:https://pan.baidu.com/s/16NCuC-mD4-3dxfVdcIFkxg 提取码:k3bw

  10. python之PIL模块基础功能

    Image主要是打开图片后,对图片进行编辑,主要有以下一些常用功能: 1.读取并显示图片: from PIL import Image img = Image.open("H:\\salar ...