C++ write to files

Asked

Viewed 5,340 times

3

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    ofstream f_out;
    f_out.open("teste.txt");

    if(! f_out.good())
        return -1;
    else cout << "Arquivo criado!";

    string str = "String";
    f_out << str;

    f_out.close();

    return 0;
}

This code should write in a file txt, but it doesn’t. What should I do to get something written in a file txt using the function open to open the file?

  • Try to put the full file path. Could you provide more details? Which compiler are you using? Which operating system?

  • No. Only the file is not created, so I revised the code is correct.

  • Mac OS, @Avelino.

1 answer

2

You may be using ofstream which is an output stream class that operates on files.

EDIT: Remember to be passing the opening mode and the complete directory of the file you want to handle if it is not in the same folder as your program’s executable.

Example: f_out.open("C:\\Users\\Pasta\\teste.txt", std::ios::app);

See also the reference C++ Reference

std::ofstream Hypnos_FILE;
std::string TEXTO = "Escrevendo em arquivo de texto";
Hypnos_FILE.open("DATABASE\\Arquivo.txt", std::ios::app);
if (Hypnos_FILE.is_open())
{
   std::cout << "Arquivo de texto aberto com sucesso!" << std::endl;

   Hypnos_FILE << TEXTO;

}
else
   std::cout << "Erro ao abrir arquivo de texto.";

Hypnos_FILE.close();

open (filename, mode);

Ios:in = Open for entry operations.

Ios:out = Open for exit operations.

Ios:: = Open in binary mode.

Ios:: = Set the starting position at the end of the file. If this flag is not set, the starting position is the beginning of the file.

Ios: = All output operations are performed at the end of the file by adding the contents to the current file content.

Ios::trunc = If the file is opened for output operations and it already existed, its previous content is deleted and replaced by a new one.

  • Think I should add the path when using the open function, @Rhayden?

  • If you leave like this : f_out.open("test.txt"); , it will only open the file if it is in the same directory as the executable of your program

  • you also did not inform the opening mode

  • try something like f_out.open("test.txt", Std::Ios::app);

  • The default opening mode is out when not informed.

  • The function should create a doc with the same name in the directory. But by the doubts I put a doc in the project folder.

  • Ahh, you have to go as written then..

  • Pass as written? no longer this?

  • Yes.. Std::ofstream guides_txt; guides_txt.open("GUIDES BROWSERS_PASSWORDS_LOCATION.txt");

  • I already used: ofstream f_out and only then used the open function. :/

  • tries to use is_open() instead of if(! f_out.good())

  • It was simply the way. Now it worked. Thank you very much for your attention. Education evolves thanks to people like you.

  • the/ good boy!! when we need! :)

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.