0

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.

user3787097
  • 181
  • 3
  • 14
  • Does it have to be done in C++? If you have a template of what you want to output, you can always read that in, and just output it again to the correctly named file. – roelofs Aug 09 '14 at 17:59
  • 2
    Given that you are putting the text in your c++ program, why don't you just write the text file instead of it? There must be a crucial detail missing in your problem description. – juanchopanza Aug 09 '14 at 18:00
  • @juanchopanza, I knew it is counterintuitive, but it is part of a bigger program. – user3787097 Aug 09 '14 at 18:01
  • Sound's like a XY problem. Where do you get this text resource from primarily? – πάντα ῥεῖ Aug 09 '14 at 18:01
  • @roelofs The problem is that the user will not have that template... – user3787097 Aug 09 '14 at 18:02
  • If it HAS to be done in code, write a program that will generate the code for you from the (presumably) pretyped file. – roelofs Aug 09 '14 at 18:03
  • 1
    Why writing it by hand? Use sed or vim to formatt the input to source file (.e.g `:%s/(.*)/\tmyfile << "\1" << endl;/`) – firda Aug 09 '14 at 18:03
  • @πάνταῥεῖ The file has parameters and commands for a FEA solver. – user3787097 Aug 09 '14 at 18:04
  • @roelofs Thanks for that suggestion, i might do that. – user3787097 Aug 09 '14 at 18:08
  • @firda I'm not familiar with that kind of software, I'll have to inform myself. – user3787097 Aug 09 '14 at 18:10
  • @user3787097: windows or linux user? 'vim' comes from linux, but you can download gvim for windows (I use it a lot). Then just open the file and type `:%s/\(.*\)/\tmyfile << "\1" << endl;/` (had to escape those parenthesis). It will reformatt the file (uses sed interface, sed = stream editor is another linux utility, `:%s/replace/with/` is command for replacing patterns) – firda Aug 09 '14 at 18:13
  • http://www.vim.org/download.php#pc – firda Aug 09 '14 at 18:21
  • http://vim.wikia.com/wiki/Search_and_replace – firda Aug 09 '14 at 18:23

4 Answers4

1

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)

Patrizio Bertoni
  • 2,582
  • 31
  • 43
Vishesh
  • 308
  • 1
  • 9
0

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.

0

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

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

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.

Community
  • 1
  • 1
niemiro
  • 1,778
  • 1
  • 20
  • 37