0

So I was making a file editor using c++ and it has 3 functions and it needs to call each other to work properly.But When code tries to call other functions it end abnormly .

I tried changing the order of functions but it does nothing.It will compile properly without warnings

it needs output the contents of the file.


#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;
}
ashwin vinod
  • 156
  • 1
  • 1
  • 13

3 Answers3

1

options() is not calling reader(), and reader() is not calling write(). In both cases, you are simply declaring functions, not actually calling them.

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!
    ..
}

Try this instead:

#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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

You can declare functions(not compulsory in your case) after all #include statements like:

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

You call write function as write(); reader function as reader(); Since functions are not returning anything you could change int reader() to void reader(), int write() to void write() and so on. Keep main as int main() though.

Nevus
  • 1,307
  • 1
  • 9
  • 21
0

In addition to the comments above about calling the functions, it seems like it would be good to initialize buffer as a char array as shown below:

#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;
}
Gardener
  • 2,591
  • 1
  • 13
  • 22