建设营销型网站流程,起点网站建设,中宁网站建设公司,建个自己的网站难吗#includeiostream
#includestring
//1#xff1a;引入头文件
#includefstream
using namespace std;
//把程序中的信息输出到缓冲区#xff0c;然后写到文件
void test01()
{//2:定义流对象ofstream ofs;//3:打开文件#xff0c;以写的方式打开iostream
#includestring
//1引入头文件
#includefstream
using namespace std;
//把程序中的信息输出到缓冲区然后写到文件
void test01()
{//2:定义流对象ofstream ofs;//3:打开文件以写的方式打开如果没有文件就创建ofs.open(test.txt, ios::out | ios::trunc);//4:判断是否打开成功if (!ofs.is_open()){cout 打开失败 endl;}//5:写信息ofs 姓名灰灰 endl;;ofs 年龄20 endl;//6:关闭文件ofs.close();//关闭文件并刷新缓冲区
}
//把磁盘信息输入到缓冲区然后读到程序中读文件
void test02()
{ifstream ifs;ifs.open(test.txt, ios::in);if (!ifs.is_open()){cout 打开失败 endl;}//第一种方式读取文件//一行一行读/*char buf[1024] { 0 };while (ifs buf){cout buf endl;}*///第二种方式读取文件//char buf[1024] { 0 };//while (!ifs.eof())//判断是否读到文件尾部//{// ifs.getline(buf, sizeof(buf));// cout buf endl;//}//第三种方式一个一个字符读char c;while ((c ifs.get()) ! EOF){cout c;}ifs.close();
}int main()
{test02();system(pause);return EXIT_SUCCESS;
}
c的二进制读写
using namespace std;
class maker
{
public:maker(){}maker( const char*name,int age){strcpy(this-name, name);this-age age;}public:int age;char name[64];
};
void test()
{maker m1(huihi, 20);maker m2(huifa,2);ofstream ofs;ofs.open(text.txt, ios::out |ios::trunc| ios::binary);if (!ofs.is_open()){cout 打开失败 endl;}//写ofs.write((const char*)m1, sizeof(maker));ofs.write((const char*)m2, sizeof(maker));ofs.close();
}
//读
void test02()
{ifstream ifs;ifs.open(text.txt, ios::in | ios::binary);if (!ifs.is_open()){cout 打开失败 endl;}//读maker m1;maker m2;ifs.read((char*) m1, sizeof(maker));ifs.read((char*)m2, sizeof(maker));cout name: m1.name age: m1.age endl;cout name: m2.name age: m2.age endl;
}
int main()
{test02();system(pause);return EXIT_SUCCESS;
}