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. 20175316 盛茂淞 实验一 Java开发环境的熟悉

    20175316 盛茂淞 实验一 Java开发环境的熟悉 实验目的 使用JDK编译.运行简单的Java程序 实验要求 1.建立"自己学号exp1"的目录 2.在"自己学号 ...

  2. 将JSON数据转换成JAVA的实体类

    思路:首先将JSON格式的数据转换成JSONObject,然后将JSONObject转换成Java的实体类(其中类属性包括List等类型) Java实体类: SearchFilter 类 1 publ ...

  3. IPC,Hz(Hertz) and Clock Speed

    How do we measure a CPU's work? Whether it's fast or not depends on three factors: IPC, Hz, Clock sp ...

  4. IIC通讯协议(非原创,转载他人,用于学习)

    I2C协议:1.空闲状态 2.开始信号 3.停止信号 4.应答信号 5.数据的有效性 6.数据传输 IIC详解 1.I2C总线具有两根双向信号线,一根是数据线SDA,另一根是时钟线SCL 2.IIC总 ...

  5. 关于esp32的省电模式的WiFi连接

    对于ESP32,其作为一款集成了2.4GHz WiFi和蓝牙双模块的单芯片,所有基于wifi和蓝牙开发是学习esp32的重要一环,今天WiFi原理和网络结构 可以点击链接进行详细的了解,这里就不做详细 ...

  6. 桌面管理工具 RedisDesktopManager 0.8.8

    RedisDesktopManager 0.8.8  发布,此版本更新内容如下: 改进: Show key bytes length and value bytes length #3677 修复: ...

  7. 初识Twisted(一)

    pip install Twisted 安装Twisted库 from twisted.internet import reactor #开启事件循环 #不是简单的循环 #不会带来任何性能损失 rea ...

  8. IPC_管道

    1.管道特点: 1)单向数据通信 2)匿名管道-常用于(父子进程/有血缘关系的进程之间) 3)命名管道-常用于(无血缘关系进程之间通信) 4)提供一种流式服务(发送和接受不接受字节数的大小,可取任意大 ...

  9. 7.AOP编程

    注解和xml混合使用 1.将所有的bean都配置xml中 <bean id="" class=""> 2.将所有的依赖都使用注解 @Autowire ...

  10. HTML中的文本标签

    <span></span> 请使用 <span> 来组合行内元素,以便通过样式来格式化它们. 注释:span 没有固定的格式表现.当对它应用样式时,它才会产生视觉上 ...