我写了一个文件读写器 继续代码还没有完成。
#include <iostream>
#include <fstream>
#include <string>
const std::string path_to_data = "\\data";
const std::string level_file = path_to_data+"\\level_file.lev";
typedef std::string (*f_read)(std::ifstream& fin,const std::string& level_file,int line_num);
typedef std::string (*f_write)(std::ofstream& fout,const std::string& level_file, std::string& what_to_write,int line_num);
typedef std::string (*pt_f_manger)(f_read,f_write);
inline bool is_empty(std::ifstream& pFile){return pFile.peek() == std::ifstream::traits_type::eof();}
std::string file_manger(f_read,f_write);
std::string read_file(std::ifstream& fin,const std::string& level_file,int line_num);
std::string write_file(std::ofstream& fout,const std::string& level_file,std::string what_to_write, int line_num);
std::string error_handler(std::string error_type,pt_f_manger f_manger);
int main()
{
std::string error = file_manger(read_file,write_file);
std::cout<<error<<"\n";
std::cin.get();
std::cin.get();
return 0;
}
std::string file_manger(f_read f_input,f_write f_output)
{
std::string good = "good";
std::string error = "error";
std::ifstream fin;
std::ofstream fout;
std::string test = "test";
std::string output = f_output(fout,level_file,test,0);
std::string input = f_input(fin,level_file,0);
if (output != "good")
return error;
if (input != "good")
return error;
return good;
}
std::string write_file(std::ofstream& fout,const std::string& level_file, std::string& what_to_write,int line_num)
{
const char* level_filec = level_file.c_str();
std::ifstream fin;
fin.open(level_filec);
if (fin.is_open())
return "failed to open "+level_file;
if (is_empty(fin))
{
fin.close();
fout.open(level_filec);
if (!fout.is_open())
return "failed to open "+level_file;
fout<<what_to_write;
fout.close();
return "good";
}
int i = 0;
int num = 0;
char ch;
while (fin.good() && i != line_num)
{
fin.get(ch);
if (ch == '\n')
++num;
++i;
}
int pos = fin.tellg();
fin.close();
fout.open(level_filec,std::ios_base::in);
if (!fout.is_open())
return "failed to open "+level_file;
fout.seekp(pos);
fout<<what_to_write;
fout.close();
return "good";
}
std::string read_file(std::ifstream& fin,const std::string& level_file,int line_num)
{
const char* level_filec = level_file.c_str();
std::string read;
fin.open(level_filec);
if (fin.is_open())
return "failed to open "+level_file;
if (is_empty(fin))
{
fin.close();
return "file is empty";
}
int i = 0;
int num = 0;
char ch;
while (fin.good() && i != line_num)
{
fin.get(ch);
if (ch == '\n')
++num;
++i;
}
std::getline(fin,read);
return read;
}
std::string error_handler(std::string error_type,pt_f_manger f_manger)//work in progress
{
return "good";
}
我收到此错误
C:\ Users \ john \ Documents \ main.cpp ||在函数'int main()'中:| C:\ Users \ john \ Documents \ main.cpp | 25 |错误:来自'std :: string(*)的无效转换(std :: ofstream&amp;,const string&amp;,std :: string,int){aka std: :basic_string(*)(std :: basic_ofstream&amp;,const std :: basic_string&amp;,std :: basic_string,int)}'to'f_write {aka std :: basic_string(*)(std :: basic_ofstream&amp;,const std: :basic_string&amp;,std :: basic_string&amp;,int)}'[-fpermissive] | C:\ Users \ john \ Documents \ main.cpp | 17 | error:初始化'std :: string file_manger(f_read,f_write)'[-fpermissive] |的参数2 || ===构建完成:2个错误,0个警告(0分0秒)=== |
我正在使用code :: blocks。
答案 0 :(得分:4)
typedef std::string (*f_write)(
std::ofstream& fout, const std::string& level_file,
std::string& what_to_write, int line_num);
std::string write_file(
std::ofstream& fout, const std::string& level_file,
std::string what_to_write, int line_num);
你能发现差异吗? (提示:比较第三个参数的类型)。