Best way to create a large text file?

This is a discussion on Best way to create a large text file? within the C++ Programming forums, part of the General Programming Boards category; I'm new to C++, and I need to code a program that will create a text file with a large ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    Best way to create a large text file?

    I'm new to C++, and I need to code a program that will create a text file with a large amount of text (at least 75k worth). There can be no seperate resource files in the final app, just the single executable.


    What's the best way to do this?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    ? details ?

    What do you need in this text file? Random Data? Data retrieved from another source? Created with an algorithm? No details = no helpful solutions.

    Provide more insight into the problem, so that we may be better able to assist.

    edit: 75k isnt really a "large" text file.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    It's a fixed set of data. If there's a way, I can simply make it part of the source code. It doesn't need to be generated or supplied externally. I know what it is at the time of compilation. I just need the program to be able to generate it into a new text file.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    So why not just zip the file in an SFX?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Because that's not the only thing the program will do. It's just the only thing I don't know how to do.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    cout << "This is the first line of the text file" << endl;

    Rinse and repeat - how is this hard?

    A fairly decent code editor should be able to make short work of this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Quote Originally Posted by Salem View Post
    cout << "This is the first line of the text file" << endl;

    Rinse and repeat - how is this hard?
    Are you kidding me? For 75k worth of text?

    Are you really telling me that's the best way of doing this in C++?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    You're the one that doesn't want to use an external file - otherwise it would be that easy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Or one line of perl....
    Code:
    $ cat t1.txt 
    Lamarck was the first man whose conclusions on the subject excited much
    attention. This justly celebrated naturalist first published his views
    in 1801; he much enlarged them in 1809 in his "Philosophie Zoologique",
    and subsequently, 1815, in the Introduction to his "Hist. Nat. des
    Animaux sans Vertebres". In these works he up holds the doctrine that
    all species, including man, are descended from other species. He first
    did the eminent service of arousing attention to the probability of
    all change in the organic, as well as in the inorganic world, being the
    result of law, and not of miraculous interposition. Lamarck seems
    to have been chiefly led to his conclusion on the gradual change of
    species, by the difficulty of distinguishing species and varieties,
    by the almost perfect gradation of forms in certain groups, and by
    
    $ perl -n -e '{ chomp; s/\"/\\"/g; print "cout << \"" . $_ . "\" << endl;\n"; }' t1.txt
    cout << "Lamarck was the first man whose conclusions on the subject excited much" << endl;
    cout << "attention. This justly celebrated naturalist first published his views" << endl;
    cout << "in 1801; he much enlarged them in 1809 in his \"Philosophie Zoologique\"," << endl;
    cout << "and subsequently, 1815, in the Introduction to his \"Hist. Nat. des" << endl;
    cout << "Animaux sans Vertebres\". In these works he up holds the doctrine that" << endl;
    cout << "all species, including man, are descended from other species. He first" << endl;
    cout << "did the eminent service of arousing attention to the probability of" << endl;
    cout << "all change in the organic, as well as in the inorganic world, being the" << endl;
    cout << "result of law, and not of miraculous interposition. Lamarck seems" << endl;
    cout << "to have been chiefly led to his conclusion on the gradual change of" << endl;
    cout << "species, by the difficulty of distinguishing species and varieties," << endl;
    cout << "by the almost perfect gradation of forms in certain groups, and by" << endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Quote Originally Posted by Salem View Post
    You're the one that doesn't want to use an external file - otherwise it would be that easy.
    That's the requirement of the app. I came here to ask how to do something, not to argue about why I'm doing it, whether or not I should do it, etc...

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Quote Originally Posted by Salem View Post

    Or one line of perl....

    Is there any way to duplicate that functionality in C++? Or perhaps to add a text file in the compiling process and somehow be able to extract it during runtime?

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Whatever way you go, there is going to be some intermediate step between a simple text file, and something you can compile with a C++ compiler.

    You can't simply do
    Code:
    const char myTextFile[] = {
    #include "anyRawTextFile.txt"
    };
    and have it compile.

    Now you can generate (in all sorts of ways) a file you can #include using the above method, but there is no standard way of doing this. Maybe a perl script as above, or maybe your compiler comes with some kind of "resource editor" which is used to attach "media data" (of all kinds) to an executable.

    Eg
    perl foo.pl anyRawTextFile.txt > anyRawTextFile.h

    Then you can do
    Code:
    const char myTextFile[] = {
    #include "anyRawTextFile.h"
    };
    And you will have the text file compiled into the code as one big string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Anything that would fit on one of those old a floppy disks can't really be called "large" any more.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by KenLP View Post
    That's the requirement of the app. I came here to ask how to do something, not to argue about why I'm doing it, whether or not I should do it, etc...
    Well, the problem is that you have not provided any details of exactly what you need.

    What's going to be in the file?
    Is it random text characters?
    Stuff entered by users?
    Stuff from another file?
    Source code?

    And yes it does matter.

    There is no programmer on earth who can code a solution to a problem he doesn't understand.
    In your case that could be "beginneritis"
    In our case it's because you seem bent upon not telling us what you're doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intentially create program with large run-time memory
    By dsollen in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2009, 12:07 PM
  2. [Large file][Value too large for defined data type]
    By salsan in forum Linux Programming
    Replies: 11
    Last Post: 02-05-2008, 04:18 AM
  3. Replies: 1
    Last Post: 08-06-2007, 11:09 PM
  4. Searching a VERY large text file
    By Tankndozer in forum C Programming
    Replies: 4
    Last Post: 07-29-2004, 03:45 AM
  5. create a text file with data using text editor
    By fried egg in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 09:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21