C/C++语言是一种通用的编程语言,具有高效、灵活和可移植等特点。C语言主要用于系统编程,如操作系统、编译器、数据库等;C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统、图形用户界面、嵌入式系统等。C/C++语言具有很高的效率和控制能力,但也需要开发人员自行管理内存等底层资源,对于初学者来说可能会有一定的难度。

标准输出流: 首先我们演示标准的输入输出,其需要引入头文件<iostream>

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> using namespace std; int main(int argc, char* argv[])
{
char str[] = "lyshark";
int number = 0; cout << "hello: " << str << endl;
cin >> number;
if (number == 0)
{
cerr << "Error msg" << endl; // 标准的错误流
clog << "Error log" << endl; // 标准的日志流
} int x, y;
cin >> x >> y; // 一次可以接受两个参数
freopen("./test.log", "w", stdout); // 将标准输出重定向到文件 system("pause");
return 0;
}

格式化输出: 在程序中一般用cout和插入运算符“<<”实现输出,cout流在内存中有相应的缓冲区。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip> using namespace std; int main(int argc, char* argv[])
{
cout << hex << 100 << endl; // 十六进制输出
cout << dec << 100 << endl; // 十进制输出
cout << oct << 100 << endl; // 八进制输出
cout << fixed << 10.053 << endl; // 单浮点数输出
cout << scientific << 10.053 << endl; // 科学计数法 cout << setw(10) << "hello" << setw(10) << "lyshark" << endl; // 默认两个单词之间空格 cout << setfill('-') << setw(10) << "hello" << endl; // 指定域宽,输出字符串,空白处以'-'填充 for (int x = 0; x < 3; x++)
{
cout << setw(10) << left << "hello" ; // 自动(left/right)对齐,不足补空格
} cout << endl;
system("pause");
return 0;
}

单个字符输出: 流对象中,提供了专用于输出单个字符的成员函数put

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip> using namespace std; int main(int argc, char* argv[])
{
char *str = "lyshark"; for (int x = 6; x >= 0; x--)
cout.put(*(str + x)); // 每次输出一个字符
cout.put('\n'); system("pause");
return 0;
}

标准输入流: 通过测试cin的真值,判断流对象是否处于正常状态.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip> using namespace std; int main(int argc, char* argv[])
{
float grade; while (cin >> grade)
{
if (grade >= 85)
cout << grade << " good" << endl;
}
system("pause");
return 0;
}

读取字符串: 通过getline函数,实现了从输入流中读取一行字符

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip> using namespace std; int main(int argc, char* argv[])
{
char str[20];
int x, y, z; cin >> x >> y >> z;
cout << x << y << z; cin.getline(str, 20); // 读入字符遇到\n结束读取
cout << str << endl; cin.getline(str, 20, 'z'); // 读入字符遇到z字符才结束
cout << str << endl; system("pause");
return 0;
}

IO流实现文件读写: 使用<fstream>文件处理类,实现对文本文件的读写操作.

#include <iostream>
#include <string>
#include <fstream> using namespace std; void Write_File(string path, string text)
{
ofstream ofs;
//::app = 打开文件后追加 ::trunc = 打开文件后清空
ofs.open(path, ios::out | ios::app);
// 判断是否打开成功,成功则写入
if (ofs.is_open())
ofs << text << endl;
} void Read_File(string path)
{
ifstream ifs; ifs.open(path, ios::in);
if (ifs.is_open())
{
char buffer[1024];
while (!ifs.eof())
{
ifs.getline(buffer, sizeof(buffer));
cout << buffer << endl;
}
}
} int main(int argc, char *argv[])
{
Write_File("c:/lyshark.log", "hello lyshark");
Write_File("c:/lyshark.log", "hello admin");
Read_File("c:/lyshark.log"); system("pause");
return 0;
}

字符串与数字判断: 判断用户输入的是数字还是字符串,并输出到屏幕上.

#include <iostream>
#include <string> using namespace std; int main(int argc, char *argv[])
{
char ch = cin.peek(); // 获得输入字符
if (ch >= '0' && ch <= '9')
{
int number;
cin >> number;
cout << "number is :" << number << endl;
}
else
{
char buffer[1024];
cin >> buffer;
cout << "string is: " << buffer << endl;
} system("pause");
return 0;
}

输入数字判断案例: 让用户输入数字如果是1-10之间的则结束,否则继续循环

#include <iostream>
#include <string> using namespace std; int main(int argc, char *argv[])
{
int number; while (true)
{
cin >> number;
if (number > 0 && number <= 10)
{
cout << "number is: " << number << endl;
break;
}
cin.clear(); // 重置标志位
cin.sync(); // 清空缓冲区
} system("pause");
return 0;
}

IO流实现排序并存盘: 将文件in.log中的整数排序后输出到out.log中.

#include <iostream>
#include <cstdlib>
#include <fstream> using namespace std; // 使用qsort排序的回调函数
int MyCompare(const void *x, const void *y)
{
return *((int *)x) - *((int *)y);
} int main(int argc, char *argv[])
{
int Array[10] = { 0 };
int index = 0;
// in.log 存放十个数 ==> 5 6 9 2 0 1 3 6 7 9
ifstream srcFile("c://in.log", ios::in); //以文本模式打开in.txt备读
ofstream destFile("c://out.txt", ios::out); //以文本模式打开out.txt备写 // 以空格为单位读出文件中的数据放入数组
int tmp;
while (srcFile >> tmp)
Array[index++] = tmp; // 调用排序函数
qsort(Array, 10, sizeof(int), MyCompare); // 输出排序后的数,并写入文件保存
for (int x = 0; x < 10; x++)
{
cout << "Array => " << Array[x] << endl;
destFile << Array[x] << " "; // 回写到文件
} srcFile.close();
destFile.close(); system("pause");
return 0;
}

读/写二进制流结构: 假设我们定义student结构,我们使用二进制方式写入到文件中.

#include <iostream>
#include <fstream> using namespace std; struct Student
{
char szName[20];
int age;
}; int main(int argc, char *argv[])
{
struct Student stu; strcpy(stu.szName, "lyshark");
stu.age = 33; // 将结构中的数据写入到磁盘
ofstream outFile("c://student.log", ios::out | ios::binary);
outFile.write((char *)&stu, sizeof(stu));
outFile.close(); // 从文件流中读取数据
struct Student read_stu;
ifstream inFile("c://student.log", ios::in | ios::binary);
inFile.read((char *)&read_stu, sizeof(read_stu));
cout << read_stu.szName << read_stu.age << endl;
inFile.close(); system("pause");
return 0;
}

利用IO流实现文件拷贝: 通过使用get函数实现,从文件中一次读取一个字节,并将其拷贝到另一个文件中.

#include <iostream>
#include <fstream> using namespace std; int main(int argc, char *argv[])
{
ifstream inFile("c://lyshark.exe", ios::binary | ios::in); //以二进制读模式打开文件
ofstream outFile("c://test.exe", ios::binary | ios::out); //以二进制写模式打开文件 char ch;
while (inFile.get(ch)) // 每次读取一个字符
{
//cout << ch << " ";
outFile.put(ch); // 每次写入一个字符
}
outFile.close();
inFile.close(); system("pause");
return 0;
}

8.1 C++ 标准输入输出流的更多相关文章

  1. IO流(三)__字节流 标准输入输出流 转换流

    一.字节流:FileInputStream 和FileOutputStream 基本操作和字符流类相同,没有flush,但是close还是要的 复制一个字节流文件 private static voi ...

  2. 黑马程序员——JAVA基础之标准输入输出流

    ------- android培训.java培训.期待与您交流! ---------- 标准输入输出流: System中的基本字段,in,out 它们各代表了系统标准的输入和输出设备. 默认输入设备是 ...

  3. freopen()——重定向标准输入输出流

    freopen()——重定向标准输入输出流 头文件:stdio.h 函数原型:FILE * freopen(const char *filename , const char *type ,  FIL ...

  4. Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)

    1.操作基本数据类型的流     1) 操作基本数据类型 · DataInputStream:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型.应用程序可以使用数据输出 ...

  5. Java标准输入输出流的重定向及恢复

    在Java中输入输出数据一般(图形化界面例外)要用到标准输入输出流System.in和System.out,System.in,System.out默认指向控制台,但有时程序从文件中输入数据并将结果输 ...

  6. Java精选笔记_其他IO流(ObjectInputStream、DataInputStream、PrintStream、标准输入输出流)

    其他IO流 ObjectInputStream和ObjectOutputStream 如果希望永久将对象转为字节数据写入到硬盘上,即对象序列化,可以使用ObjectOutputStream(对象输出流 ...

  7. Java IO流-标准输入输出流

    2017-11-05 19:13:21 标准输入输出流:System类中的两个成员变量. 标准输入流(public static final InputStream in):“标准”输入流.此流已打开 ...

  8. 序列流、对象操作流、打印流、标准输入输出流、随机访问流、数据输入输出流、Properties(二十二)

    1.序列流 * 1.什么是序列流 * 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.* 2.使用方式 * 整合两个 ...

  9. java 标准输入输出流,打印流,数据流

    1 package stream; import static org.junit.Assert.assertNotNull; import java.io.BufferedReader; impor ...

  10. 【.net 深呼吸】通过标准输入/输出流来完成进程间通信

    实现进程之间煲电话粥的方式,有好几种,比如,你可以用这些方案: 1.使用socket来传递.这个好像很无聊,本地进程之间也用socket?不过,通过本机回环网络确实可以进程之间通信. 2.WCF,与上 ...

随机推荐

  1. 用Python自动清理系统垃圾,再也不用360安全卫士了

    用Python自动清理系统垃圾,再也不用360安全卫士了 在Windows在安装和使用过程中都会产生相当多的垃圾文件,包括临时文件(如:.tmp.._mp)日志文件(.log).临时帮助文件(.gid ...

  2. spring中这些编程技巧,真的让我爱不释手

    前言 最近越来越多的读者认可我的文章,还是挺让人高兴的.有些读者希望我多分享spring方面的知识点,能够在实际工作中派的上用场.我对spring的源码有过一定的研究,结合我这几年实际的工作经验,把s ...

  3. SpringCloud学习 系列二、 简介

    系列导航 SpringCloud学习 系列一. 前言-为什么要学习微服务 SpringCloud学习 系列二. 简介 SpringCloud学习 系列三. 创建一个没有使用springCloud的服务 ...

  4. Flume原理及使用案例

    本文为转载篇!原文: https://www.cnblogs.com/zhangyinhua/p/7803486.html https://www.cnblogs.com/ciade/p/549521 ...

  5. HTTP 及 http 请求解析过程

    本文为博主原创,未经允许不得转载: HTTP 全称为:超文本传输协议(HyperText Transfer Protocol,HTTP),一种无状态的,以请求/应答方式运行的协议, 它使用可扩展的语义 ...

  6. 基于html5+javascript技术开发的房贷利率计算器,买房的码农们戳进来

    房贷计算器是一款专为购房者设计的实用工具应用,其主要功能是帮助用户详细计算房贷的还款金额.利息以及还款计划等.通过这款软件,用户可以更加便捷地了解到自己的还款情况和计划,从而更好地规划自己的财务.下面 ...

  7. Ubuntu Linux下的PDF阅读器推荐——Okular

    安装方法 在Ubuntu下直接使用sudo apt-get install okular即可,如果中间遇到依赖项的问题,可以通过运行sudo apt --fix-broken install来自动修复 ...

  8. 使用华为路由连接WiFi,被限制网速?

    1.问题 使用华为路由连接WiFi,但是网速非常慢,经常半天才能加载一个界面? 2.解决方法 登陆http://192.168.3.1/网址,发现限速一栏被框选,取消即可

  9. 在Winform系统开发中,使用MediatR来实现类似事件总线的消息处理

    MediatR是一款进程内的消息订阅.发布框架,可实现请求/响应.命令.查询.通知和事件的消息传递,解耦了消息处理器和消息之间耦合.提供了Send方法用于发布到单个处理程序.Publish方法发布到多 ...

  10. [转帖]Linux内存之Cache

    一. Linux内存之Cache 1.1.Cache 1.1.1.什么是Cache? Cache存储器,是位于CPU和主存储器DRAM之间的一块高速缓冲存储器,规模较小,但是速度很快,通常由SRAM( ...