自测题1的第一个大题和一些c++文件的读写

发布时间 2023-05-05 22:24:17作者: 赵百万

//#include<iostream>
//#include<fstream>
//#include<cmath>
//using namespace std;
//class MyComplex
//{
//private:
// double x, y;
//public:
// MyComplex() { x = 0; y = 0; }
// MyComplex(double value1, double value2)
// {
// x = value1;
// y = value2;
// }
// double getX(void)
// {
// return x;
// }
// double getY(void)
// {
// return y;
// }
// double Mod_Com(void)
// {
// return (double)sqrt(x * x + y * y);
// }
// MyComplex operator+(MyComplex a)
// {
// return MyComplex(this->x + a.x, this->y + a.y);
// }
//};
//int main()
//{
// MyComplex A;
// double m, n;
// cin >> m >> n;
// MyComplex B(m, n);
// MyComplex C = A + B;
// cout << A.Mod_Com() << endl;
// cout << B.Mod_Com() << endl;
// cout << C.getX() <<"+" << C.getY() << "i";
// ofstream outfile;
// outfile.open("rew.txt", ios_base::_Noreplace|ios_base::out);
// outfile << A.Mod_Com() <<endl << B.Mod_Com() <<endl<< C.getX() << "+" << C.getY() << "i";
// outfile.close();
// return 0;
//}
////#include <fstream>
////#include <iostream>
////using namespace std;
////
////int main()
////{
////
//// char data[100];
////
//// // 以写模式打开文件
//// ofstream outfile;
//// outfile.open("afile.dat");
////
//// cout << "Writing to the file" << endl;
//// cout << "Enter your name: ";
//// cin.getline(data, 100);
////
//// // 向文件写入用户输入的数据
//// outfile << data << endl;
////
//// cout << "Enter your age: ";
//// cin >> data;
//// cin.ignore();
////
//// // 再次向文件写入用户输入的数据
//// outfile << data << endl;
////
//// // 关闭打开的文件
//// outfile.close();
////
//// // 以读模式打开文件
//// ifstream infile;
//// infile.open("afile.dat");
////
//// cout << "Reading from the file" << endl;
//// infile >> data;
////
//// // 在屏幕上写入数据
//// cout << data << endl;
////
//// // 再次从文件读取数据,并显示它
//// infile >> data;
//// cout << data << endl;
////
//// // 关闭打开的文件
//// infile.close();
////
//// return 0;
////}
////fstream提供三个类
////ifstream从已有的文件读入
////ofstream向文件写内容
////fstream打开文件供写读
////ios_base::app打开一个输出文件用于在文件结尾添加数据
////ios_base::ate打开一个现存文件(用于输出或输入)并查找到结尾
////ios_base::in打开一个输入文件,对于一个ofstream文件,使用ios_base::in作为一个open-mode可避免删除一个现存文件的所有内容,只读
////ios_base::out打开一个文件,用于输出,对于所有ofstream对象,只写
////ios_base::trunc打开一个文件,如果它已经存在则删除其中原有的所有内容
////ios_base::binary以二进制打开一个文件
////ios_base::_Noreplace打开一个文件如果文件不存在创建文件
////ios_base::_Nocreate打开一个文件如果文件不存在不创建文件
////ios::beg文件头
////ios::end文件尾
////ios::cur当前位置
////file.seekg(0,ios::beg)文件指针定位到文件开头
////file.seekg(0,ios::end)文件指针定位到文件结尾
////file.seekg(10,ios::cur)文件指针从当前位置向文件末移动是个字节
////file.seekg(-10,ios::cur)文件指针从当前位置向文件开始移动是个字节
////file.seekg(10,ios::beg)文件指针定位到离文件开头10个字节的位置
//// 可以用或把属性连接起来,如outfile.open("文件名",ios::out|ios::binary)打开以二进制写入
//// outfile.write((char*)&stdu[0],sizeof(stud))
//// stdu为结构体如
//// struct Stdu
//// {
//// char name[10];
//// ine age;
//// }stdu;
////#include <fstream>
////#include <iostream>
////using namespace std;
////int main()
////{
//// char data[100];
//// ofstream outfile;// 以写模式打开文件
//// outfile.open("afile.dat");
//// cout << "Writing to the file" << endl;
//// cout << "Enter your name: ";
//// cin.getline(data, 100);//输入数组,用法cin.getline(数组名,字符个数,结束标志)
//// outfile << data << endl;// 向文件写入用户输入的数据
//// cout << "Enter your age: ";
//// cin >> data;
//// cin.ignore();//清除缓存区中的数据
//// outfile << data << endl;// 再次向文件写入用户输入的数据
//// outfile.close();// 关闭打开的文件
//// ifstream infile;// 以读模式打开文件
//// infile.open("afile.dat");
//// cout << "Reading from the file" << endl;
//// infile >> data;
//// cout << data << endl;// 在屏幕上写入数据
//// infile >> data;// 再次从文件读取数据,并显示它
//// cout << data << endl;
//// infile.close();// 关闭打开的文件
//// return 0;
////}
////结构体读入还没弄明白
//#include <fstream>
//#include <iostream>
//#include<cstring>
//using namespace std;
//int main()
//{
// ofstream a;
// //a.open("mmm",ios_base::trunc);//清空
// a.open("mmm", ios_base::out | ios_base::app);
// string m = "aaabbb11";
// string q = "qqqq";
// a << m<<q;
// a.close();
// ifstream c;
// c.open("mmm", ios_base::in);
// c >> m >> q;
// cout << m << q;
// a.close();
// return 0;
//}