- No space between the functiona name and parentheses.
- Don't use "fstream". You're outputting to this stream, so use the ofstream class instead. Also, this is an output file, but you use something that's commonly seen as "file in". Bad programmer!
code: | ofstream output_file; |
- Why separately call openimmediately after the declaration? Just let the constructor do the work for you.
code: | ofstream output_file("files/titles.txt",ios::out|ios::app); |
- You're using a char array? That's C madness. This is C++ and we have proper strings.
- You're using cout without a namespace qualifier and \n instead of std::endl. Madness.
code: | std::cout << "Enter the data you want to insert inside the file" << std::endl; |
- Again, using cin without a qualifier...
- Since we're now using strings that know their own length we don't have to use some crazy write function.
code: | output_file << data; |
- The close function is ok.
- One last thing: you declare and initialize the output file before you actually use it in your function. C+ doesn't force you to do this.
code: | void write()
{
std::string data;
std::cout << "Enter the data you want to insert inside the file" << std::endl;
std::cin >> data;
std::ofstream output_file("files/titles.txt",ios::out|ios::app);
output_file << data;
output_file.close();
} |
|