user3787097
user3787097

Reputation: 181

Creating the same text file over and over

I need to create a program that writes a text file in the current folder, the text file always contains the same information, for example:

Hello,
This is an example of how the text file may look
some information over here
and here
and so on

So I was thinking in doing something like this:

#include <iostream>
#include <fstream>

using namespace std;

int main(){
    ofstream myfile("myfile.txt");
    myfile << "Hello," << endl;
    myfile << "This is an example of how the text file may look" << endl;
    myfile << "some information over here" << endl;
    myfile << "and here" << endl;
    myfile << "and so on";

    myfile.close();
    return 0;
}

Which works if the number of lines in my text file is small, the problem is that my text file has over 2000 lines, and I'm not willing to give the myfile << TEXT << endl; format to every line.

Is there a more effective way to create this text file? Thanks.

Upvotes: 0

Views: 147

Answers (4)

niemiro
niemiro

Reputation: 1838

It sounds like you should really be using resource files. I won't copy and paste all of the information here, but there's a very good Q&A already on this website, over here: Embed Text File in a Resource in a native Windows Application

Alternatively, you could even stick the string in a header file then include that header file where it's needed: (assuming no C++11 since if you do you could simply use Raw to make things a little easier but an answer for that has already been posted - no need to repeat).

#pragma once
#include <iostream>

std::string fileData =
    "data line 1\r\n"
    "data line 2\r\n"
    "etc.\r\n"
;

Use std::wstring and prepend the strings with L if you need more complex characters. All you need to do is to write a little script (or even just use Notepad++ if it's a one off) to replace backslashes with double backslash, replace double quotation marks with backslash double quotation marks, and replace line breaks with \r\n"{line break}{tab}". Tidy up the beginning and end, and you're done. Then just write the string to a file.

Upvotes: 0

Jarod42
Jarod42

Reputation: 218268

You may use Raw string in C++11:

const char* my_text =
R"(Hello,
This is an example of how the text file may look
some information over here
and here
and so on)";

int main()
{
    std::ofstream myfile("myfile.txt");
    myfile << my_text;
    myfile.close();
    return 0;
}

Live example

Alternatively, you may use some tools to create the array for you as xxd -i

Upvotes: 0

Vishesh
Vishesh

Reputation: 306

If you have the problem of writing in same file, you need to use an append mode. i.e., your file must be opened like this

ofstream myfile("ABC.txt",ios::app)

Upvotes: 1

user3160514
user3160514

Reputation:

If you don't care about the subtile differences between '\n' and std::endl, then you can create a static string with your text outside of your function, and then it's just :

myfile << str // Maybe << std::endl; too

If your text is really big, you can write a small script to format it, like changing every newlines with "\n", etc.

Upvotes: 0

Related Questions