Uffo
Uffo

Reputation: 10046

Creating files in C++

I want to create a file using C++, but I have no idea how to do it. For example I want to create a text file named Hello.txt.

Can anyone help me?

Upvotes: 104

Views: 459823

Answers (7)

Angelo
Angelo

Reputation: 51

#include <iostream>
#include <fstream>
#include <string>


std::string filename = "/tmp/filename.txt";

int main() {
  std::ofstream o(filename);

  o << "Hello, World\n";

  return 0;
}

This is what I had to do in order to use a variable for the filename instead of a regular string.

Upvotes: 4

Tachi
Tachi

Reputation: 617

If you want to create a file with some content and don't need to deal with the ofstream after that you can simply write:

#include <fstream>

int main() {
    std::ofstream("file.txt") << "file content";
}

no need to manually close the file, deal with variables, etc. The file is created, written, and closed in the same line.

Upvotes: 1

Boiethios
Boiethios

Reputation: 42789

Do this with a file stream. When a std::ofstream is closed, the file is created. I prefer the following code, because the OP only asks to create a file, not to write in it:

#include <fstream>

int main()
{
    std::ofstream { "Hello.txt" };
    // Hello.txt has been created here
}

The stream is destroyed right after its creation, so the stream is closed inside the destructor and thus the file is created.

Upvotes: 45

Vinayyy
Vinayyy

Reputation: 19

/*I am working with turbo c++ compiler so namespace std is not used by me.Also i am familiar with turbo.*/

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<fstream.h> //required while dealing with files
void main ()
{
clrscr();
ofstream fout; //object created **fout**
fout.open("your desired file name + extension");
fout<<"contents to be written inside the file"<<endl;
fout.close();
getch();
} 

After running the program the file will be created inside the bin folder in your compiler folder itself.

Upvotes: -1

George Theodosiou
George Theodosiou

Reputation: 353

Here is my solution:

#include <fstream>

int main()
{
    std::ofstream ("Hello.txt");
    return 0;
}

File (Hello.txt) is created even without ofstream name, and this is the difference from Mr. Boiethios answer.

Upvotes: 4

James Thompson
James Thompson

Reputation: 48192

One way to do this is to create an instance of the ofstream class, and use it to write to your file. Here's a link to a website that has some example code, and some more information about the standard tools available with most implementations of C++:

ofstream reference

For completeness, here's some example code:

// using ofstream constructors.
#include <iostream>
#include <fstream>  

std::ofstream outfile ("test.txt");

outfile << "my text here!" << std::endl;

outfile.close();

You want to use std::endl to end your lines. An alternative is using '\n' character. These two things are different, std::endl flushes the buffer and writes your output immediately while '\n' allows the outfile to put all of your output into a buffer and maybe write it later.

Upvotes: 169

Sean Bright
Sean Bright

Reputation: 120654

#include <iostream>
#include <fstream>

int main() {
  std::ofstream o("Hello.txt");

  o << "Hello, World\n" << std::endl;

  return 0;
}

Upvotes: 13

Related Questions