Reading (and writing) files in C++
"But how do I read data from a file ....?"
. Question on a C++ course
on which we had concentrates, thus far, on object orientation.
1. Include the fstream
2. Create an object of type ifstream
using the constructor, and passing in the file name
3. Read lines from the ifstream
object using the getlines
4. Check for end of file using the eof
if (infile.eof()) break;
with the latter two statements in a loop. Complete source [here]
Writing a file? use an ofstream
I've added a full example of input and output [here]
on our web site.
In the example, a file (named on the command line) is read in line by line, and is combed - i.e. a sample of one line in every 10 (or 15, or 20, or any other number given on the command line) is output - either to the screen, or to an output file if one is named on the command line.
The program includes extensive (but NOT complete) data validation, checking whether the files open correctly, and whether the correct number of parameters were given on the command line.
We offer a range of C and C++ courses - for newcomers to programming, as well as to delegates with prior programming experience. See [here]
(written 2012-07-18, updated 2012-08-11)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesC235 - C and C based languages - I/O in C++ 
Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21) 
Printing objects in C++ - (2011-08-13) 
C++ - unknown array size, unknown object type. Help! - (2011-04-17) 
C++ - putting the language elements together into a program - (2011-01-08) 
Comparing Objects in C++ - (2008-06-13) 
Some new C programming examples - files, structs, unions etc - (2007-12-19)
Some other Articles
Associated Classes - using objects of one class within anotherDwarf Exception Unwind InfoCan you put names to faces?Reading (and writing) files in C++2011 Census results - initial figures for Wiltshire.Scenes from commuting by trainA Walk on the South BankWhat a difference a year makes A year ago today, a server upgrade and a new Perl example
4214 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).