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

This is a discussion on C++: Output An Entire Text File- Can It Be Done? within the C++ Programming forums, part of the General Programming Boards category; hello!!! Is it possible to read in to a program an entire ".txt" file, instead of reading information individually into ...

  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
    20,968
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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: reading text

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, 02: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

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