#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#define N 20
using namespace std;
//标准输入输出
void test2()
{
//标准输入
//double a, b;
//cin >> a >> b;
//char ch = cin.get();
//putchar(ch);
//getchar();
//cout << endl
//cout << "second called:" << endl;
//cin.get(ch);
//cout << ch << endl;
//getchar();
//char str[10];
//cout << "third call:";
//cin.get(str, 6, '4');
//cout << str << endl;
//getline()函数
//char str[N];
//cout << "Enter a sentenc:";
//cin.getline(str, 10, '/');
//cout << "The first part" << str << endl;
//cin.getline(str, 10);
//cout << "The second part " << str << endl;
//标准输出 ostream
//cout.put(字符变量或者常量)
//char ch = 'c';
//cout.put(ch);
//cout.put('a').put('c').put('d') << endl;
//输出格式
//输出格式的控制字符
//dec 十进制 hex 十六进制 oct 八进制
//setw(n)设置输出项域宽
//int i = 1024, j = 256,a=18;
//double p = 3.155544, q = 3.14;
//cout << i << endl;
//cout << setw(15) << i << j << endl;
//cout << hex << i <<endl;
//cout << j << endl;
//cout << a<< endl;
//cout << hex << j << endl;
//cout << setw(10) << setiosflags(ios::left) << setfill('#') << i << endl;
//cout << setw(6) << setfill('*') <<i<< endl;
//cout << p << endl;
//cout << setprecision(5) << p << endl;
//cout << p << endl;
//cout << resetiosflags(ios::left) << setw(10) << setfill('#') << j << endl;
//常见输出格式的成员函数
// width(n)设置当前高度n
//fill(c)填充字符c
//precision(n)设置浮点数精度n
//int i = 1024;
//double j = 13.14155427;
//cout << i << endl;
//cout.width(10);
//cout << i << endl;
//cout << i << endl;
//cout.width(10);
//cout.fill('$');
//cout.setf(ios::left, ios::adjustfield);
//cout << i << endl;
//cout.width(12);
//cout.setf(ios::right, ios::adjustfield);
//cout.precision(5);
//cout << j << endl;
//cout << "width" << cout.width() << endl;
//文件的输入输出
//打开文件方式
// ios::app 追加模式
//ios::ate 打开文件时文件指针定位到文件尾
// ios::in 打开文件进行读操作 不存在报错
//ios::out 打开文件进行写操作 已存在更新
//ios::trunc 打开文件 如果文件已经存在则清空原文件
//ios::binary 二进制文件
// 读文件
// ifstream infile;
// infile.open(字符串路径,打开文件方式);
或者
// ifstream infile(字符串路径, 打开文件方式);
// 读文件
// ofstream outfile;
// outfile.open(字符串路径,打开文件方式);
或者
// ifstream outfile(字符串路径, 打开文件方式);
//关闭文件
//outfile.close()或者infile.close()
//顺序文件访问
//向输出文件中写入文本
//fstream ofs;
//ofs.open("d:\\fl.txt", ios::out);
//if (ofs.fail())
//{
// cout << "错误" << endl;
//}
//ofs << setiosflags(ios::left) << setw(13) << "Name" << setw(10) << "Class" << setw(10) << "Age" << endl;
//ofs << setiosflags(ios::left) << setw(12) << "liu yanchao" << setw(10) << endl;
//for (char ch = 'a';ch < 'z';ch++)
//{
// ofs.put(ch);
//}
//ofs << endl;
//ofs.write("sdafasdfas", 11);
//ofs.close();
//读取文件内容到显示器
//fstream ifs("d:\\fl.txt", ios::out);
//if (ifs.fail())
//{
// cout << "error" << endl;
//}
//while (! ifs.eof())
//{
// cout.put((char)ifs.get());
//}
//用二进制方式写入数据
//fstream ofs;
//ofs.open("d:\\fl.txt", ios::out | ios::binary);
//if (!ofs)
//{
// cout << "错误" << endl;
//}
//double number[] = { 1.12,12.12,1.23,232.2 };
//for (int i = 0;i < 4; i++)
//{
// ofs.write((char *) & number[i], sizeof(double));
//}
//ofs.close();
//用二进制方式读数据
//fstream ifs("d:\\fl.txt",ios::in|ios::binary);
//if (!ifs)
//{
// cout << "错误" << endl;
//}
//double z[4];
//for (int i = 0;i < 4;i++)
//{
// ifs.read((char*)&z[0], sizeof(double));
// cout << z[0] << endl;
//}
//ifs.close();
//随机文件访问
//seekg()用于输入文件 格式 : seekg(offset,dir) 或者 seekg(pos)
//seekp()用于输出文件 格式 : seekp(offset,dir) 或者 seekg(pos)
//ios::beg 从文件起始位置开始
//ios::cur 从当前文件指针位置开始
//ios::end 从文件结束位置开始
//tellg() 获取此时文件指针位置信息
struct student
{
string name;
int age;
double score;
};
//student stu1[] = {
// "liu",12,90,
// "wang",15,60,
// "li",15,20
//};
//
//ofstream ofs("d:\\dl.txt", ios::out | ios::binary);
//if (!ofs)
//{
// cout << "error" << endl;
//}
//for (int i = 0;i < 3;i++) ofs.write((char*)&stu1[i], sizeof(stu1));
//ofs.close();
//ifstream ifs;
//ifs.open("d:\\dl.txt", ios::out | ios::binary);
//if (!ifs)
//{
// cout << "error" << endl;
//}
//student stu2[3];
//for (int i = 0;i < 3;i++)
//{
// ifs.seekg(i*sizeof(stu2[i]),ios::beg);
// ifs.read((char*)& stu2[i], sizeof(student));
// cout << stu2[i].name << stu2[i].age << stu2[i].score<< endl;
//}
//ifs.close();
//读取随机文件中的数据
//fstream fs("d:\\dl.txt",ios::out|ios::in|ios::binary);
//if (!fs)
//{
// cout << "错误" << endl;
//}
//for (int i = 1;i < 100;i++)
//{
// fs.write((char*)&i, sizeof(int));
//}
//int n, m;
//cin >> n;
//fs.seekg(n * sizeof(int), ios::beg);
//cout << fs.tellg() << endl;
//fs.read((char*)&m, sizeof(int));
//cout << "读出数据" << m<< endl;
//数据写入随机文件
fstream fs("d:\\dl.txt", ios::in|ios::out|ios::binary);
if (!fs)
{
cout << "erroe" << endl;
}
for (int i = 0;i <= 10;i++)
{
fs.write((char*)&i, sizeof(int));
}
int n;
cin >> n;
fs.seekp(n * sizeof(int), ios::beg);//写入设置文件指针
cout << fs.tellg() << endl;
int m = 100;
fs.write((char*)&m, sizeof(int));
fs.seekg(0, ios::beg);//写出设置文件指针
cout << fs.tellg() << endl;//输出0
int number;
for (int i = 0;i <=10;i++)
{
fs.read((char*)&number, sizeof(int));
cout << number << endl;
}
fs.close();
}
//写文件
void test()
{
ofstream ofs;
ofs.open("d:\\fl.txt", ios::binary | ios::app);
ofs << "姓名" << endl;
ofs << "性别" << endl;
ofs.close();
return;
}
//读文件
void test1()
{
ofstream ifs;
ifs.open("d:\\fl.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
}
char buf[1024] = { 0 };
}
int main()
{
test2();
return 0;
}
c++ 文件的操作
最新推荐文章于 2024-09-19 13:36:34 发布