【问题标题】:C++ function unable to another function and ends automaticlyC++ 函数无法调用另一个函数并自动结束
【发布时间】:2019-04-18 05:42:21
【问题描述】:

所以我用c++制作了一个文件编辑器,它有3个函数,需要互相调用才能正常工作。但是当代码试图调用其他函数时,它会异常结束。

我尝试更改函数的顺序,但它什么也没做。它会正确编译而不会出现警告

它需要输出文件的内容。


#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#include <string>
#include <iomanip>
#include <unistd.h>
#include <sstream>
using namespace std;/* std */

  /* data */
 char buffer;
 std::string fname;

 int reader(){
   std::ifstream readfile;
   readfile.open(fname.c_str());
   readfile>>buffer;
   std::cout << buffer<< '\n';
   int write();
   }
int options(){
  cout << "************************"<< '\n';
  cout << "* Starting File editor *"<< '\n';
  cout << "************************"<< '\n';
  cout << "*    Enter Filename    *"<< '\n';
  cin >>fname;
  cout << "Opening File"<<fname<< '\n';
  int reader();
  std::cout << buffer<< '\n';
   }
int write(){
  cout << "writing to file  " << '\n';
  std::ofstream writefile;
  writefile.open(fname.c_str());
  writefile<<buffer;
  cout << "writing done " << '\n';
   }
int main()
{
  /* code */
  options();

  return 0;
}

【问题讨论】:

  • 你为什么在reader()函数中声明write()int write();
  • 不,我试图调用该函数。
  • 缓冲区是单个字符吗?
  • 请给我一个答案
  • 不知道,我觉得不是一个字符

标签: c++ function file


【解决方案1】:

options() 不是 调用 reader()reader() 不是调用 write()。在这两种情况下,您只是声明函数,而不是真正调用它们。

int reader(){
    ...
    int write(); // <-- a declaration, not a call!
}

int options(){
    ...
    int reader(); // <-- a declaration, not a call!
    ... 
}

int main() {
    ...
    options(); // <-- a call, not a declaration!
    ..
}

试试这个:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/* data */
char buffer;
std::string fname;

int reader(){
    cout << "opening file " << fname << '\n';
    std::ifstream readfile(fname.c_str());
    readfile >> buffer;
    std::cout << buffer << '\n';
}

int write(){
    cout << "writing to file " << '\n';
    std::ofstream writefile(fname.c_str());
    writefile << buffer;
    cout << "writing done" << '\n';
}

int options(){
    cout << "************************"<< '\n';
    cout << "* Starting File editor *"<< '\n';
    cout << "************************"<< '\n';
    cout << "* Enter Filename *"<< '\n';
    cin >> fname;
    reader();
    write();
}

int main() {
    /* code */
    options();
    return 0;
}

【讨论】:

  • 那么我该如何调用函数。
  • @ashwinvinod 请参阅我发布的示例。比较它的原始代码。你看出区别了吗?
【解决方案2】:

除了上面关于调用函数的 cmets 之外,似乎最好将 buffer 初始化为 char 数组,如下所示:

#include <iostream>
#include <fstream>
//#include <bits/stdc++.h>
#include <string>
#include <iomanip>
#include <unistd.h>
#include <sstream>
using namespace std;/* std */

/* data */
char buffer[]{"Short test"};
std::string fname;
void write(){
  cout << "writing to file  " << '\n';
  std::ofstream writefile;
  writefile.open(fname.c_str());
  writefile<<buffer;
  cout << "writing done " << '\n';
}
void reader(){
  std::ifstream readfile;
  readfile.open(fname.c_str());
  readfile>>buffer;
  std::cout << buffer<< '\n';
  write();
}
void options(){
  cout << "************************"<< '\n';
  cout << "* Starting File editor *"<< '\n';
  cout << "************************"<< '\n';
  cout << "*    Enter Filename    *"<< '\n';
  cin >>fname;
  cout << "Opening File"<<fname<< '\n';
   reader();
  std::cout << buffer<< '\n';
}

int main()
{
  /* code */
  options();

  return 0;
}

【讨论】:

    【解决方案3】:

    您可以在所有 #include 语句之后声明函数(在您的情况下不是强制性的),例如:

    int reader();
    int write();
    int options();
    

    您将写入函数称为write(); 读取函数称为reader(); 由于函数没有返回任何内容,您可以将int reader() 更改为void reader(),将int write() 更改为void write() 等等。保持 main 为 int main()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多