Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
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.

In summary:
1. Include the fstream header file
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 method
4. Check for end of file using the eof method

In code:
  #include <fstream>
  ifstream infile("requests.xyz");
  infile.getline(myline,256);
  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] for details.

(written 2012-07-18, updated 2012-08-11)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C235 - C and C based languages - I/O in C++
  [4563] Formatting and outputting your own classes in C++ - (2015-10-30)
  [4562] Left shift operator on an output stream object - C++ - (2015-10-30)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3390] Printing objects in C++ - (2011-08-13)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [1675] Comparing Objects in C++ - (2008-06-13)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)


Back to
2011 Census results - initial figures for Wiltshire.
Previous and next
or
Horse's mouth home
Forward to
Can you put names to faces?
Some other Articles
Associated Classes - using objects of one class within another
Dwarf Exception Unwind Info
Can you put names to faces?
Reading (and writing) files in C++
2011 Census results - initial figures for Wiltshire.
Scenes from commuting by train
A Walk on the South Bank
What a difference a year makes
A year ago today, a server upgrade and a new Perl example
4759 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, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3807_Rea ... in-C-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat