C, C++ & C# in easy steps by Mike McGrath
Author:Mike McGrath
Language: eng
Format: epub
Publisher: In Easy Steps
Published: 2023-07-07T00:00:00+00:00
Writing a file
The ability to read and write files from a program provides a useful method of maintaining data on the computerâs hard disk. The format of the data may be specified as human-readable plain text format or machine-readable binary format.
The standard C++ <fstream> library provides functions for working with files, which can be made available by adding an #include <fstream> directive at the start of the program.
For each file that is to be opened, a filestream object must first be created. This will be either an âofstreamâ (output filestream) object, for writing data to the file, or an âifstreamâ (input filestream) object, for reading data from the file. The ofstream object is used like the cout function that writes to standard output, and the ifstream object works like the cin function that reads from standard input.
The declaration of a filestream object for writing output begins with the ofstream keyword, then a chosen name for that particular filestream object followed by parentheses nominating the text file to write to. So, the declaration syntax looks like this:
ofstream object-name ( âfile-nameâ ) ;
The argument nominating the text file may optionally contain the full file path, such as âC:\data\log.txtâ or â/home/user/log.txtâ, otherwise the program will seek the file within the directory in which the program resides.
Before writing output to a file, the program should always first test that the filestream object has actually been created. Typically, this is performed by an if statement that allows the program to write output only when the test is successful.
If a nominated file already exists, it will by default be overwritten without warning. Otherwise, a new file will be created and written.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Games Book by Huw Davies(658)
Kylian Mbappe by Ed Hawkins(260)
Timelines of Science: From Fossils to Quantum Physics by Dorling Kindersley(192)
Merry Christmas by Uncle Amon(179)
The Magical World of BDSM by Ganesha Andrey(173)
Boy Like Me: A Searing YA Novel About Two Boys Finding Love in the Shadow of Section 28 by Simon James Green(170)
The Lion Storyteller Book of Animal Tales by Bob Hartman(158)
C, C++ & C# in easy steps by Mike McGrath(158)
The Big Gay Alphabet Coloring Book by Jacinta Bunnell(157)
Every One Is Special: Cats by Fiona Munro(155)
The Shar-Wolf of New York City by Aleesah Darlison(147)
The Wheel of Life by Ellen Glasgow(144)
Let's Play and Learn Together: Fill Your Baby's Day with Creative Activities that are Super Fun and Enhance Development by Roni Leiderman(141)
Product Operations: How successful companies build better products at scale by Melissa Perri & Denise Tilles(138)
Small Acts of Kindness by Caroline Day(132)
The Epic of Kings, Hero Tales of Ancient Persia by Firdausi(130)
A Narrative of a Nine Months' Residence in New Zealand in 1827 by Augustus Earle(126)
Time-Out from Technology by Molly Wigand(125)
Artificial Ingredients by Scherer Lauri S.;(125)
