c++输出输入流
来源:互联网 发布:人工智能学校排名 编辑:程序博客网 时间:2024/04/27 08:50
我想我还没到那个水平去仔细研究每个的工作原理,下面的这些就是一个总结,很基础的东西。
一. 输出流
1. cout 这个东西我们用的太多了。他是重载了 <<符号。
2. cerr对应标准错误流,不进入缓冲区,也就是说它是直接到达屏幕,不需要等待缓冲和换行字符。
3. clog 和cerr一样,唯一区别就是clog要进入缓冲区。
4. put()函数,cout对象的成员函数。cout.put();用于输出单个字符。
二. 输入流
1. cin cin>>a;
2. cin.get() 类似c中的getchar()。
1)无参get() 获得标准输入流中的一个字符。返回值就是获取的那个字符,若遇到结束符,那么就返回EOF,也就是-1。
2)一个参数的get(a),从标准输入流中获得一个字符,赋值给a,如果成功返回非0,如果失败返回0;
3)三个参数的get(字符数组或指针,字符个数n,终止字符)。在标准输入流中获取n-1个字符,赋值给字符数组或指针,如果遇到终止符号,就停止读取。
char c[20];cin.get(c, 10, '\n');cout << c << endl;3. cin.getline(字符数组或指针,字符个数n,终止字符) 和 get(字符数组或指针,字符个数n,终止字符)一样。
4. cin.eof();表示文件结束,0为假,非0为真
5. cin.peek()当前输入流指针的下一个字符
6. cin.putback() 将get或者getline中读取的字符a返回输入流,插入到当前指针位置
7. cin.ignore(n,终止符) 从当前指针开始(不包含当前指针),忽略标准输入流后面n个字符,或者遇到终止符,就不再往后忽略(终止符也会被忽略)
三 . 文件的操作
ofstream: 文件写操作,内存写入存储设备
ifstream:文件读操作,内存读取存储设备
fstream: 读写操作,对打开的文件进行读写操作
ifstream in("...", ios::in);
fstream foi("...", ios::in|ios::out);
首先来看看打开文件的函数:
<span style="font-size:14px;">void open (const char* filename, ios_base::openmode mode = ios_base::out); //c++98</span>
</pre><pre name="code" class="cpp">//c++11
void open (const char* filename, ios_base::openmode mode = ios_base::out);void open (const string& filename, ios_base::openmode mode = ios_base::out);下面是mode的取值:
如果open的时候文件不存在,那么会自动生成该文件。
当然,也可以不需要手动调用open函数,因为在其构造函数中会调用open函数,mode是默认的打开方式,但是这样直接使用需要调用is_open函数判断是否打开
关闭文件:
void close();当我们对文件进行操作之后,我们需要把该文件关闭,已恢复其可以访问的状态。
所以在操作完成之后别忘记调用这个函数。
文件的读写:
<pre name="code" class="cpp"><pre name="code" class="cpp">#include <fstream>using namespace std;int main(){ofstream out("text.txt");if (out.is_open()){out << "This is a text. \n";out << "This is a bug. \n";out.close();}char text[256];ifstream in("text.txt");if (!in.is_open()){cerr << "Error Open File! \n";exit(1);}while (!in.eof()){in.getline(text, 100);cout << text << endl;}return 0;}
对于二进制文件,有两个独立的函数进行读写:
读:read ( char * buffer, streamsize size );
写:write ( char * buffer, streamsize size );
这里的buffer是一块内存的地址。作用是存储或者读取数据的。
这里的buffer是一块内存的地址。作用是存储或者读取数据的。
#include <fstream>using namespace std;class Student{private:char _id[10];char _name[10];int _grade;public:Student(char *id, char *name, int grade){strcpy(_id, id);strcpy(_name, name);_grade = grade;}Student():_id("11111"),_name(""),_grade(0){}~Student(){}Student(Student &student){strcpy(_id, student._id);strcpy(_name, student._name);_grade = student._grade;}friend ostream & operator << (ostream & out, Student &student){out << student._id << endl;out << student._name << endl;out << student._grade << endl;return out;}};int main(){Student student[5] = { Student("010","liming",80),Student("011","lihua",90),Student("012","litian",85),Student("013","lizheng",80),Student("014","lixu",95) };Student s1[5];ofstream out("text.dat", ios::binary | ios::out);if (out.is_open()){for (size_t i = 0; i < 5; ++i){out.write((char *)(&student[i]), sizeof(student[i]));}out.close();}ifstream in("text.dat", ios::binary | ios::in);if (!in.is_open()){cerr << "Open File Error!";exit(1);}for (size_t i = 0; i < 5; ++i){in.read((char *)(&s1[i]), sizeof(s1[i]));cout << s1[i];}in.close();return 0;}
0 0
- C语言流输入和输出函数
- C++IO流-标准输入与输出
- C程序设计语言--输入/输出
- C --- 文件输入/输出
- C格式化输入/输出
- C++:输入与输出
- 【C】标准输入/输出
- C格式化输入、输出
- [C++]输入/输出流类库
- C输入与输出
- C 输入 & 输出
- C语言 文字输出输入
- c语言 输入/输出函数
- C的输入和输出
- C/C++输入,输出汇总
- C语言文件输入/输出
- C程序设计----输入和输出
- C语言格式化输入/输出
- 轻乳酪蛋糕
- WINDOWS DOS命令大全
- spring加载hibernate映射文件的几种方式
- How to add Oracle JDBC driver in your Maven local repository
- 直接启动SDK Manager: $ADNROID_HOME/tools/android
- c++输出输入流
- Linux android studio :'tools.jar' seems to be not in Android Studio classpath.
- Apache Zepplin 安装详解
- Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理分析
- 保留重复出现的字符串
- 剑指 offer:调整数组顺序使奇数位于偶数前面
- 数据存储--SharedPreferences--仿QQ登陆界面记住密码效果
- 小技巧,使用低版本的VS打开高版本的解决方案(使用VS2008打开VS2010的解决方案--同样适用于其他版本)
- 比较好的博客链接