c++ io 学习
2013-08-22 14:52
183 查看
1. 几种读取文件的方式 get(char&), >>(char*), getline(char*, capacity);
#include <iostream>
#include <fstream>
using namespace std;
void main(){
ofstream file("main.txt");
file <<"hello stephen"<< endl;
file <<"your first name is xing"<< endl;
file.close();
char ch;
ifstream file2("main.txt");
while (!file2.eof()){
file2.get(ch);
cout << ch;
}
file2.close();
cout << "======================================" << endl;
file2.open("main.txt");
char str[30];
while (!file2.eof()){
file2 >> str;
cout << str;
}
file2.close();
cout << "======================================" << endl;
file2.open("main.txt");
char strLine[1024];
while (!file2.eof()){
file2.getline(strLine, 1024);
cout << strLine << endl;
}
file2.close();
}
2.文件的几种错误状态,有所了解即可
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ofstream File1("file2.txt"); //建立file2.txt
File1.close();
//下面的检测代码将会返回错误,这是因为我使用了ios::noreplace打开模式
//它模式在试图打开一个已存在的文件时会返回错误
ofstream Test("file2.txt",ios::_Noreplace);
//上一行将导致ios::failbit错误,我们这就将其演示出来
if(Test.rdstate() == ios::failbit){
cout << "Error, not fatal error...!\n";
}
Test.clear(ios::goodbit); //将当前状态重置为ios::goodbit
if(Test.rdstate() == ios::goodbit) {//检测程序是否已经正确地施行了设置
cout << "Fine!\n";
}
Test.clear(ios::eofbit); //将状态标志设为ios::eofbit. 无实际用途.
if(Test.rdstate() == ios::eofbit) {//检测是否已经正确地施行了设置
cout << "EOF!\n";
}
Test.close();
}
#include <iostream>
#include <fstream>
using namespace std;
void main(){
ofstream file("main.txt");
file <<"hello stephen"<< endl;
file <<"your first name is xing"<< endl;
file.close();
char ch;
ifstream file2("main.txt");
while (!file2.eof()){
file2.get(ch);
cout << ch;
}
file2.close();
cout << "======================================" << endl;
file2.open("main.txt");
char str[30];
while (!file2.eof()){
file2 >> str;
cout << str;
}
file2.close();
cout << "======================================" << endl;
file2.open("main.txt");
char strLine[1024];
while (!file2.eof()){
file2.getline(strLine, 1024);
cout << strLine << endl;
}
file2.close();
}
2.文件的几种错误状态,有所了解即可
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ofstream File1("file2.txt"); //建立file2.txt
File1.close();
//下面的检测代码将会返回错误,这是因为我使用了ios::noreplace打开模式
//它模式在试图打开一个已存在的文件时会返回错误
ofstream Test("file2.txt",ios::_Noreplace);
//上一行将导致ios::failbit错误,我们这就将其演示出来
if(Test.rdstate() == ios::failbit){
cout << "Error, not fatal error...!\n";
}
Test.clear(ios::goodbit); //将当前状态重置为ios::goodbit
if(Test.rdstate() == ios::goodbit) {//检测程序是否已经正确地施行了设置
cout << "Fine!\n";
}
Test.clear(ios::eofbit); //将状态标志设为ios::eofbit. 无实际用途.
if(Test.rdstate() == ios::eofbit) {//检测是否已经正确地施行了设置
cout << "EOF!\n";
}
Test.close();
}
相关文章推荐
- C++ Primer高速学习 第一章 获得二:输入和输出 (IO)
- C++IO系统学习
- C++ 学习笔记 <2> C++ IO
- C++的io学习
- 从Android/Java基础之上学习C/C++语言(7)--C语言基础--IO
- 【C++ 学习】C++输入输出IO
- 【C++学习】【阶段一】指针&文件IO
- C++学习7-面向对象编程基础(多态性与虚函数、 IO文件流操作)
- c++ 学习笔记 c++ io
- C++学习笔记:IO操作
- C++学习笔记:IO操作
- C++学习_IO:输入/输出
- C++开源库学习地址总结
- C++学习第二、第三天
- ICE学习(二)-Ice编程 C++
- C++学习之域运算符 ::
- C/C++实现微信跳一跳辅助(学习纪录)
- unix_c++学习笔记
- 学习C++.Primer.Plus 5 循环和关系表达式
- java IO学习