C++的标准IO
cin istream stdin
cout ostream stdout
cerr(无缓冲) ostream stderr
clog(有缓冲) ostream stderr
printf scanf 格式化标准输入输出
typedef basic_istream<char> istream;
typedef basic_ostream<char> ostream;
typedef basic_iostream<char> iostream;
fprintf fscanf 格式化文件输入输出
typedef basic_ifstream<char> ifstream;
typedef basic_ofstream<char> ofstream;
typedef basic_fstream<char> fstream;
sprintf sscanf 格式化字符串输入输出
typedef basic_istringstream<char> istringstream;
typedef basic_ostringstream<char> ostringstream;
typedef basic_stringstream<char> stringstream;
格式化输入输出
<< >> 格式化输入输出运算符
showbase 用来输出进制前缀
showpoint 显示小数点
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main(){
cout << true << ":" << false << endl;
cout << boolalpha << true << ":" << false << endl;
cout << 0xffff << endl;
cout << showbase << hex << 0xffff << endl;
cout << showbase << oct << 0xffff << endl;
cout << showbase << dec << 0xffff << endl;
cout << 3.14151926438971626 << endl;
cout << 3.14151926438971626 << endl;
double d = 3.0;
cout << d << endl;
cout << showpoint << d << endl;
string s;
cin >> s;
cout << s << endl;
char c;
cin >> c;
cout << c << endl;
cout.fill('*');
cout << setw(40) << "hello" << endl;
cout << right << setw(40) << "hello" << "world" << endl;
cout << left << setw(40) << "hello" << "world" << endl;
cout << internal << setw(40) << "hello" << "world" << endl;
return 0;
}
非格式化输入输出(字符)
put/get
cout.put('a');
char c;
cin.clear();
cout<<"input char:";
cin.ignore();//忽略/n
cin.get(c);
#include <iostream>
using namespace std;
int main(){
int d;
cout << "input a integer:";
cin >> d;
cout << "d = " << d << endl;
cout.put('a');
cout.put(65);
char c;
cin.clear();
cout << "input a char:";
cin.ignore();
cin.get(c);
cout.put(c);
char str[100] = {};
cin >> str;
cout << str << endl;
return 0;
}
随机IO
(其实就是fseek)
tellp/seekp; 写位置 ofstream fstream
tellg/seekg; 读位置 ifstream
seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。
对于第一个参数,可以是正负数值,正的表示向后偏移,负的表示向前偏移。而第二个参数可以是:
ios::beg:表示输入流的开始位置
ios::cur:表示输入流的当前位置
ios::end:表示输入流的结束位置
tellg()函数不需要带参数,它返回当前定位指针的位置,也代表着输入流的大小。
#include <iostream>
#include <fstream>
using namespace std;
int main(){
fstream fs("a.txt",ios::in|ios::out|ios::trunc);
if(!fs){
cout << "fstream fs error" << endl;
}
int x = 1024;
fs << "hello wrold" << endl;
fs << x << endl;
double d = 3.1415926;
fs << d << endl;
fs.seekg(0,ios::beg);
char str[100];
fs.getline(str,100,'\n');
cout << str << endl;
cout << fs.tellg() << endl;
cout << fs.tellp() << endl;
fs.seekp(0,ios::end);
cout << fs.tellg() << endl;
cout << fs.tellp() << endl;
fs.close();
return 0;
}
二进制IO
write/read;
cin.read(str,100);//读指定个数,没到数目不停止
cin.ignore();
cin.getline(str,100);//读到/n结束
cin>>str;//这种就是遇到空格结束
#include <iostream>
#include <fstream>
using namespace std;
class FileCopy{
private:
ifstream ifs;
ofstream ofs;
public:
FileCopy(string src,string dest):ifs(src.c_str()),ofs(dest.c_str()){
if(!ifs.good()){
throw string("打开文件失败");
}
if(!ofs.good()){
ifs.close();
throw string("打开文件失败");
}
}
~FileCopy(){
ifs.close();
ofs.close();
}
void copy(){
char line[10] = {};
while(!ifs.eof()){
ifs.read(line,10);
ofs.write(line,ifs.gcount());
}
}
};
int main (int argc,char *argv[]){
if(argc < 3){
cout << argv[0] << " srcfile destfile " << endl;
return -1;
}
FileCopy fco(argv[1],argv[2]);
fco.copy();
return 0;
}
文件输入输出
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream ofs("a.txt");
ofs << "Hello world" << endl;
int n = 1024;
ofs << n << endl;
double d = 3.14159;
ofs << d << endl;
ofs.close();
return 0;
}
文件复制
#include <iostream>
#include <fstream>
using namespace std;
class FileCopy{
private:
ifstream ifs;
ofstream ofs;
public:
FileCopy(string src,string dest):ifs(src.c_str()),ofs(dest.c_str()){
if(!ifs.good()){
throw string("打开文件失败");
}
if(!ofs.good()){
ifs.close();
throw string("打开文件失败");
}
}
~FileCopy(){
ifs.close();
ofs.close();
}
void copy(){
char line[10] = {};
while(!ifs.eof()){
ifs.read(line,10);
ofs.write(line,ifs.gcount());
}
}
};
int main (int argc,char *argv[]){
if(argc < 3){
cout << argv[0] << " srcfile destfile " << endl;
return -1;
}
FileCopy fco(argv[1],argv[2]);
fco.copy();
return 0;
}
简易的学生管理
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
class Stu{
private:
int no;
string name;
int s[3];
public:
Stu(){}
Stu(int no):no(no){}
Stu(int no,string name,int s[]){
this->no = no;
this->name = name;
memcpy(this->s,s,sizeof(this->s));
}
bool operator==(const Stu& s)const{
return no == s.no;
}
friend ostream& operator<<(ostream& os,const Stu& s);
friend istream& operator>>(istream& is,Stu &s);
};
ostream& operator<<(ostream& os,const Stu& s){
os << s.no << " " << s.name;
for(int i=0;i<3;i++)
os << " " << s.s[i];
return os<<endl;
}
istream& operator>>(istream& is,Stu &s){
is >> s.no >> s.name;
for(int i=0;i<3;i++)
is >> s.s[i];
return is;
}
void load(vector<Stu>& vs){
ifstream ifs("stu.dat");
while(!ifs.eof()){
Stu s;
ifs >> s;
if(!ifs.eof()){
vs.push_back(s);
cout << s ;
}
}
ifs.close();
}
void save(vector<Stu>& vs){
ofstream ofs("stu.dat",ios::out|ios::trunc);
vector<Stu>::iterator it;
for(it=vs.begin();it!=vs.end();it++){
ofs << *it;
}
}
void add(vector<Stu>& vs){
Stu s;
cout << "no name s[3]:";
cin >> s;
vs.push_back(s);
}
void list(vector<Stu>& vs){
vector<Stu>::iterator it;
cout << "no name s" << endl;
for(it=vs.begin();it!=vs.end();++it){
cout << *it;
}
}
void find(vector<Stu>& vs){
int no;
cout << "input stu's no:";
cin >> no;
Stu s(no);
vector<Stu>::iterator it = find(vs.begin(),vs.end(),s);
if(it != vs.end()){
cout << *it;
}else{
cout << "查无此人" << endl;
}
}
void del(vector<Stu>& vs){
int no;
cout << "input stu's no:";
cin >> no;
Stu s(no);
vector<Stu>::iterator it = find(vs.begin(),vs.end(),s);
if(it != vs.end()){
cout << *it;
vs.erase(it);
}else{
cout << "查无此人" << endl;
}
}
void mod(vector<Stu>& vs){
Stu s;
cout << "input stu:";
cin >> s;
vector<Stu>::iterator it = find(vs.begin(),vs.end(),s);
if(it != vs.end()){
cout << *it;
*it = s;
cout << *it;
}else{
cout << "查无此人" << endl;
}
}
void run(){
vector<Stu> vs;
load(vs);
while(true){
cout << "成绩管理系统" << endl;
cout << "1.增加学生" << endl;
cout << "2.列出" << endl;
cout << "3.查找" << endl;
cout << "4.删除" << endl;
cout << "5.修改" << endl;
cout << "0.退出" << endl;
cout << "input:";
int opt = 0;
cin >> opt;
switch(opt){
case 1:
add(vs);
break;
case 2:
list(vs);
break;
case 3:
find(vs);
break;
case 4:
del(vs);
break;
case 5:
mod(vs);
break;
case 0:
save(vs);
return;
}
}
}
int main(){
run();
return 0;
}
格式化输入输出
#include <sstream>
ostringstream buf(s1);
istringstream is(s);
#include <iostream>
#include <sstream>
using namespace std;
int main(){
int x = 1024;
double d = 3.14159;
char s1[100] = {};
ostringstream buf(s1);
buf << x << " " << d << " Hello world";
cout << buf.str() << endl;
string s = "9527 4.444 hello java";
istringstream is(s);
is >> x;
is >> d;
string ss;
cout << x << endl;
cout << d << endl;
char srr[20] = {};
is.getline(srr,20,'\0');
cout << srr << endl;
return 0;
}
多文件编程
提多文件,就先提之前遇到的一个问题,A,B类函数中相互会构造对方,这样会报错,我们用多文件的思想来解决
也就是类里面声明,类外::实现
#include <iostream>
using namespace std;
class B;
class A{
int x;
int y;
public:
void show(B& b);
};
class B{
int a;
int b;
public:
void func(A& a){
}
};
void A::show(B& b){
b.func(*this);
}
int main(){
A a;
B b;
a.show(b);
return 0;
}
下面是一个多文件系统的基本流程 先写一个.h
#ifndef _STU_H__
#define _STU_H__
#include <iostream>
using namespace std;
extern const int x;
namespace SMS{
class Stu{
private:
int no;
string name;
int s;
static int st;
public:
Stu(int no=0,string name="",int s=0);
void show();
bool operator==(const Stu& s)const;
static void bar();
friend ostream& operator<<(ostream& os,const Stu& s);
friend istream& operator>>(istream& is,Stu& s);
};
ostream& operator<<(ostream& os,const Stu& s);
istream& operator>>(istream& is,Stu& s);
}
#endif
再写一个.cpp
#include "stu.h"
using namespace SMS;
const int x = 1024;
int Stu::st = 111;
Stu::Stu(int no,string name,int s):no(no),name(name),s(s){
}
void Stu::show(){
cout << "show" << endl;
}
bool Stu::operator==(const Stu& s)const{
return no == s.no;
}
void Stu::bar(){
cout << "bar" << endl;
}
ostream& SMS::operator<<(ostream& os,const Stu& s){
return os << s.no << " " << s.name << " " << s.s;
}
istream& SMS::operator>>(istream& is,Stu& s){
return is >> s.no >> s.name >> s.s;
}
写一个main.cpp
#include "stu.h"
using namespace SMS;
int main(){
Stu s(110,"李三",90);
cout << x << endl;
return 0;
}
gcc main.o stu.o
|