C++学习笔记(7)

1.创建一个顺序文件与文件输入(追加)

#include<iostream>

#include<fstream>

#include<string>

using namespace std;

 

int main()

{

   ofstream file; //注意是ofstream而不是ifstream,二者不同,见下

   file.open("1.txt", ios::out);//ios::app表示追加;ios::ate可以在文件的任何位置写数据;ios::in表示输入(但是此时文件必须是存在的,而且输入的回逐一替换以前的,但是不会一次性将以前的全部删除);ios::out表示输出(但是会删除已有内容)

   if (!file)

   {

      cerr << "File Open Error !" << endl;

      exit(1);

   }

   cout << "input the account,name,and balance" << endl;

   int account;

   string name;

   double balance;

   //当一个指针作为判定条件时,C++将一个空指针视为FALSE,非空为TRUE,这里隐式的调用了cin成员运算符void*;

   while (cin >> account >> name >> balance)

   {

      file << account << ' ' << name << ' ' << balance << endl;

   }//最终输入control+Z终止

   file.close();//当不需要时 可以显示的调用该函数来减少资源占用

}

2.从顺序文件中读取数据

#include<iostream>

#include<fstream>

#include<string>

#include<iomanip>

using namespace std;

 

void Print(int account, const string name, double balance)

{

   cout << left << setw(10) << account << setw(13) << name << setw(13) << balance << endl;

}

int main()

{

   ifstream file("1.txt", ios::in);

   if (!file)

   {

      cerr << "file open error !" << endl;

      exit(1);

   }

   cout << left << setw(10) << "Account" << setw(13) << "Name" << setw(13) << "Balance" << /*fixed << showpoint <<*/ endl;

   int account;

   string name;

   double balance;

   while (file >> account >> name >> balance)

   {

      Print(account, name, balance);

   }

   return 0;

}

重定位文件定位指针,istream和ostream都提供了成员函数来重定位文件定位指针(文件中下一个被读取或写入的字节号);在istream中,这个成员函数为seekg(),在ostream中为seekp();每个istream对象都有一个读取指针来指出文件中下一个输入的字节号,每个ostream对象都有一个写入指针来指出文件中下一个被输出的字节号;如:file.seekg(0)将定位指针重定位于文件的起始位置,其中还可以指定第二个参数来指定寻找的方向,可以是ios::beg(默认)(相对于流的开始位置进行定位);ios::end(相对于文件的末尾);ios::cur(相对于文件的当前位置)(注意数值代表的是文件开始位置到当前位置的字节数);如:file.seekg(n,ios::cur)

同样的操作可以使用osream中的seekp来实现;

成员函数tellg和tellp分别用于返回当前的“读取”和“写入”指针的位置

3.更新顺序文件:如果要将300  White  0.00数据替换成300  Worthington  0.00 的话,由于Worthington比White多了六个字符,则从“o”之后的字符就会覆盖文件中下一条顺序记录的开头;如果要使用这种方法来更新数据的话就只有用两个文件,先将该数据之前的数据拷贝到新文件中,让后再替换,之后再拷贝该数据之后的数据过去即可

实例:反复读取文件

#include<iostream>

#include<fstream>

#include<iomanip>

#include<string>

#include<stdlib.h>

using namespace std;

 

enum RequestType {ZERO_BALANCE = 1,CREDIT_BALANCE,DEBIT_BALANCE,END};

int getRequest();

bool shouldDisplay(int, double);

void outputLine(int, const string, double);

 

int main()

{

   //cout << ZERO_BALANCE << " " << CREDIT_BALANCE << " " << DEBIT_BALANCE << " " << END << endl;

   ifstream inClientFile("1.txt", ios::in);

   if (!inClientFile)

   {

      cerr << "File Opened Erro !" << endl;

      exit(1);

   }

   int request;

   int account;

   string name;

   double  balance;

   request = getRequest();

   while (request != END)

   {

      switch (request)

      {

      case ZERO_BALANCE:

          cout << "\nAccounts With Zero Balance :\n";

          break;

      case CREDIT_BALANCE:

          cout << "\nAccounts With Credit Balance :\n";

          break;

      case DEBIT_BALANCE:

          cout << "\nAccouts With Debit Balance :\n";

          break;

      }

      inClientFile >> account >> name >> balance;

      while (!inClientFile.eof()) //返回一个零值,直到文件结束

      {

          if (shouldDisplay(request,balance))

          {

             outputLine(account, name, balance);

          }

          inClientFile >> account >> name >> balance;

      }

      inClientFile.clear();

      inClientFile.seekg(0);

      request = getRequest();

   }

   cout << "End Of Run !" << endl;

   return 0;

}

int getRequest()

{

   int request;

   cout << "\nEnter The  Request :\n"

      << "1-List Accounts Of Zero Balance \n"

      << "2-List Accounts Of Credit Balance\n"

      << "3-List Accounts Of Debit Balance\n"

      << "4-End Of Run !" << endl;

   do

   {

      cout << "\n?";

      cin >> request;

   } while (request<ZERO_BALANCE && request>END);  //这里的条件不成立,所以只允许输入一次

   return request;

}

bool shouldDisplay(int type, double balance)

{

   if (type == ZERO_BALANCE && balance == 0)

      return true;

   if (type == CREDIT_BALANCE  &&balance < 0)

      return true;

   if (type == DEBIT_BALANCE && balance >0)

      return true;

   return false;

}

 

void outputLine(int account, const string name, double balance)

{

   cout << left << setw(10) << account << setw(13) << name << setw(13) << balance << endl;

}

 

4.随机存取文件:

  (a):创建随机存取文件

(1)ostream的成员函数write()函数可以从指定位置写入数据;istream中的read()函数可以从指定位置读取字节数据。

(2)write函数使用:file.write(reinterpret_cast<const char *>(&number),sizeof(number))  //实际上reinterpret_cast为强制类型转换,这里write函数要求const char * 类型;但是reinterpret_cast与编译器相关,所以尽量不要使用

对于read函数:file.read(reinterpret_cast<char *>(&number),sizeof(number)),要求char*类型的数据

(3)调用string类的函数将一个string数据写入到定长的char数组中(这也是为了创建随机存取文件),实例:

   string lastNameString;

   char lastName[15];

   int length = lastNameString.size();

   lastNameString.copy(lastName, length);

lastName[length] = '\0'; //记住最后还要自己加\0

(4)ofstream outFile("1.txt", ios::out | ios::binary); //ios::binary表示以二进制形式打开文件,如果要写入定长的记录,这是比要的

(5)对于既要读又要写的文件,课可以直接使用(fstream)

ifstream File("1.txt", ios::in | ios::out | ios::binary);

(6)file.clear()//表示复位文件结束指示符,一般在文件读取错误或者用户输入错误时,又要继续使用文件,此时应该调用此函数

转载于:https://www.cnblogs.com/Con-Tch-LLYF/p/6538887.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值