Thread: C++: Output An Entire Text File- Can It Be Done?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    C++: Output An Entire Text File- Can It Be Done?

    hello!!!

    Is it possible to read in to a program an entire ".txt" file, instead of reading information individually into variables!!

    Basically... i'm writing a magazine inventory program. When the user inputs some information, it is written to a text file.... then when they input another magazine, this is saved to the same txt file...etc....

    I then have to give the user the option to see all entries, within the program.

    Can I output an entire txt file to screen????

    please help me.... I may have no hair left otherwise... :-)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is it possible to read in to a program an entire ".txt" file, instead of reading information individually into variables!!
    You would read the contents of the file into say, a string variable.

    Can I output an entire txt file to screen?
    Once you have the contents on the file stored in a variable, you can print it to screen.

    Incidentally, have you considered using SQLite for an embedded relational database engine instead of writing your own flatfile database?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course you can. This is not the stone age, after all.
    Open file. Seek to end of file, get position to get size (or just use an API).
    Allocate a big enough buffer, then use istream.read to read the entire file into buffer and output to screen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Thanks!! :-)

    Thanks for that info, but I'm pretty new to C++ (studying at college).
    I think I understand what you mean.... but could u perhaps show me in code?

    in_file_mags.open(H:\\mag_store.txt)

    //thats how I open the file

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    in_file_mags.open("H:\\mag_store.txt");
    THAT is how you open a file.
    And I really don't deal with C++ I/O, so I don't know how the search functions works.
    I believe seekg seeks the read position in the file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    thanks chick, I'll have a play about :-)

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, there are a few methods mentioned in this thread: http://cboard.cprogramming.com/showthread.php?t=98219

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Function is called and I am trying to open a file
    By tommy69 in forum C Programming
    Replies: 88
    Last Post: 05-06-2004, 08:33 AM
  5. sorting characters and output to text file
    By odb1 in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2003, 04:46 PM