106 lines
2.8 KiB
C++
Raw Normal View History

2024-11-23 11:00:35 +00:00
#include <iostream>
#include <fstream>
using namespace std;
void c_io(); // c IO
void stream_io(); // c++ 流IO
void stream_fio(); // c++ 流文件IO
int main() {
c_io();
return 0;
}
void c_io() {
int i = 14;
double d = 3.3;
char c = 'e';
const char * str = "Hello";
// scanf & printf
// in
scanf("%d %lf %c %s", &i, &d, &c, &str);
// out
printf("%d---%3d---%x---%o \n", i, i, i, i); //int型
printf("%3.3f \n", d); //double型
printf("%c \n", c); //char
printf("%s \n", str); //str
printf("%p \n", i); //地址
c = getchar(); //读入一个char, 最快
putchar();
fgets();
}
void stream_io() {
// ios::sync_with_stdio(false);cin.tie(0),cout.tie(0); 关闭缓冲加速
int i = 14;
double d = 3.3;
char c = 'e';
const char * str = "Hello";
cin.get(c); //返回一个流引用
c = cin.get(); //返回char略快
cin.get(str, 100, '\n'); //读入字符串
cin.width(10); //设置场宽
}
void stream_fio() {
//文件流的创建 打开 关闭
fstream fin;
fstream fout;
fin.open("in");
fout.open("out");
fin.close();
fout.close();
/*
open函数的原型如下
void open(char const *,int filemode,int =filebuf::openprot);
3123使
2
ios::in
ios::out
ios::ate
ios::app
ios::binary
ios::trunc
ios_base::app方式之外ios_base::app使文件当前的写指针定位于文件尾
bad() truefalse
clear()
eof() truefalse
good() truefalse
fail() good相反falsetrue
is_open() truefalse
*/
// 二进制文件读写
fin.write((char*)&data, sizeof data);
fin.read((char*)&data, sizeof data);
/*
seekg(pos,ios::);
seekg(pos);
seekp(pos,ios::);
seekp(pos);
tellg();
tellp();
ios::beg
ios::cur
ios::end
*/
}