-2

我写了一个文件读取器/写入器,代码还没有完成。

#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&, const string&, std::string, int) {aka std::basic_string (*) (std::basic_ofstream&, const std::basic_string&, std::basic_string, int)}' 到 'f_write {aka std::basic_string (*)(std::basic_ofstream&, const std::basic_string&, std: :basic_string&, int)}' [-fpermissive]| C:\Users\john\Documents\main.cpp|17|错误:初始化 'std::string file_manger(f_read, f_write)' [-fpermissive]| 的参数 2 ||=== 构建完成:2 个错误,0 个警告(0 分钟,0 秒)===|

我正在使用代码::块。

4

1 回答 1

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);

你能看出区别吗?(提示:比较第三个参数的类型)。

于 2013-08-11T15:58:31.323 回答