(这也太难记了555老阔疼
- 文件读写示例:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string srsfname("asdfile.txt"); //定义源文件
ifstream srsfile(srsfname.c_str()); //打开源文件流类
if(!srsfile) //失败给出信息
{
cout << "Error:unable to open the infile:" << srsfname <<endl;
return -1;
}
string purfname("purfile.txt"); //定义目标文件
ofstream purfile(purfname.c_str()); //打开目标文件流类
if(!purfile) //失败给出信息
{
cerr << "Error:unable to open the infile:" << purfname <<endl;
return -1;
}
string buf;
while(srsfile >> buf)
{
purfile << buf; //输入到目标文件
}
return 0;
}
- get/put函数实例
#include <iostream>
using namespace std;
int main()
{
const int str_len = 100;
char str[str_len];
while(cin.get(str,str_len))
//当读入的数据不为空时循环下一次,每次最多读入str_len个
{
int count = cin.gcount(); //当前实际读入多少个字符
cout << "the number is:" << count <<endl;
if(count < str_len)
cin.ignore(); //在下一行之前去掉换行符
}
return 0;
}
- 输入运算符重载
#include <iostream>
#include <string.h>
using namespace std;
class Word
{
char *word;
size_t num; //存储的字符个数
public:
Word(const char* const str = NULL);
virtual ~Word()
{
if(word) //清除指针
delete []word;
}
friend ostream& operator<<(ostream& out, Word& sword);
};
Word::Word(const char* const str)
{
if(str != NULL)
{
num = strlen(str);
word = new char[num+1];
strcpy(word,str);
}
}
ostream& operator<<(ostream& out, Word& sword)
{
out << "<" << sword.num << ">" << sword.word <<endl;
return out;
}
int main()
{
Word word("hello");
cout << word;
return 0;
}
- 输入运算符重载
#include <iostream>
#include <string.h>
using namespace std;
class Word
{
char *word;
size_t num;
public:
Word(const char* const str = NULL);
virtual ~Word()
{
if(word)
delete []word;
}
friend ostream& operator<<(ostream& out, Word& sword);
friend istream& operator>>(istream& in, Word& sword);
};
Word::Word(const char* const str)
{
if(str != NULL)
{
num = strlen(str);
word = new char[num+1];
strcpy(word,str);
}
}
ostream& operator<<(ostream& out, Word& sword)
{
out << "<" << sword.num << ">" << sword.word <<endl;
return out;
}
istream& operator>>(istream& in, Word& sword)
{
char str[100];
in.getline(str, 100);
if(in.gcount() > 0)
{
delete []sword.word;
sword.num = strlen(str);
sword.word = new char[sword.num+1];
strcpy(sword.word, str);
}
return in;
}
int main()
{
Word word("hello");
cout << word;
cin >> word;
cout << word;
return 0;
}
-
用open打开文件
例如: ofstream out; out.open("test.tt", ios::out); //表示用输出流对象out打开一个"test.tt"文件。
-
文本文件读写
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char str[20] = "Welcome to C++!";
int i = 1234;
char ch;
ofstream out("test.tt"); //打开输出文件流
if(out) //如果成功
{
out << str << '\n' << i; //向文件输入信息
out.close(); //关闭
}
ifstream in("test.tt"); //打开输入文件流
if(in)
{
while(in.get(ch)) //输出显示
cout << ch;
cout <<endl;
in.close();
}
return 0;
}
- 二进制文件读写
#include <iostream>
#include <fstream>
using namespace std;
void myread(const char* fname)
{
ifstream file_read(fname, ios::binary);
char c;
if(file_read)
{
while(file_read.get(c)) //文件没有结束时
cout << c;
}
}
void mywrite(const char* fname)
{
ofstream file_write(fname, ios::binary);
char c = 'A';
if(file_write)
{
for(int i = 0; i < 26; i++)
file_write.put(c+i);
}
}
int main()
{
char fname[20] = "word.file";
mywrite(fname);
myread(fname);
cout <<endl;
return 0;
}
- 一组数据的文件读写
#include <iostream>
#include <fstream>
using namespace std;
void myread(const char* fname)
{
ifstream file_read(fname, ios::binary);
char c[30];
if(file_read)
{
file_read.read(c, 26*sizeof(char));
c[26] = '\0';
cout << c <<endl;
}
}
void mywrite(const char* fname)
{
char c[30];
for(int i = 0; i < 26; i++)
c[i] = 'A' + i;
ofstream file_write(fname, ios::binary);
if(file_write)
file_write.write(c, 26*sizeof(char));
}
int main()
{
char fname[20] = "word.file";
mywrite(fname);
myread(fname);
return 0;
}
- 随机访问文件实例
#include <iostream>
#include <fstream>
using namespace std;
void myread(const char* fname)
{
ifstream file_read(fname, ios::binary);
char c[10];
if(file_read)
{
file_read.seekg(1, ios::beg); //从开始位置移动1位
file_read.get(c[0]); //读取一个字符
file_read.seekg(2, ios::cur); //从当前位置移动2位
file_read.get(&c[1], 4*sizeof(char));
//一次读取1个字符,连续读取3次,最后一个'\0'
cout << c;
}
}
void mywrite(const char* fname)
{
char c[30];
for(int i = 0; i < 26; i++)
c[i] = 'A' + i;
ofstream file_write(fname, ios::binary);
if(file_write)
file_write.write(c, 26*sizeof(char));
}
int main()
{
char fname[20] = "word.file";
mywrite(fname);
myread(fname);
cout <<endl;
return 0;
}