..


Sponsored Links

Read and write text files with C + +

Article written by Damiano Verda
Page 1 of 2

Often, you may find yourself in a position to interact with a file as an input device or output for our programs.

Regarding the use of files as input, consider the example to all those cases where you need to rework a text in some way, such as ordering in a particular criterion. Or even to situations where the program is part of the valley to write a project that produces results in a file, which must therefore be read to store the results of processing carried out up to that point.

Even use a file as the output is very common, as it allows you to keep track of the results obtained by saving them to disk, thus allowing further analysis and rework, as well as being a control valve for any errors.

In this article, we will focus on tools that allow you to interact with the files as the source and destination of data, ie as the input and output. Use one of the most common programming languages, versatile and effective, or C + + .

Reading from Files






 # Include <fstream.h>









 void main ()

 





 {



  



 ifstream OpenFile ("cpp-input.txt");



  



 char ch;



  



 while (! OpenFile.eof ())



  



 {



    



 OpenFile.get (ch);



    



 court <<ch;



  



 }



  



 OpenFile.close ();







 }



The above progam allows you to read the data contained in a file, one character at a time. In fact, we will assume in this context that this is a text file, the 'adaptation to a different type of data is in any case very simple considerations and proposals are generally valid.

We examine the characteristics of the program. The first line (# include <fstream.h>) allows you to use the fstream library, dedicated to the interaction with the files.

The main function contains code that runs when launched the program. The first two lines, namely: ifstream OpenFile ("cpp-input.txt") and char ch correspond to the declaration of two variables.

The OpenFile variable of type ifstream, which represents the file will be read the data, whose name ("cpp-input.txt") is passed as a parameter (make sure the file from which read data is present in the folder program or at least to that location, otherwise we will have an error at run time) and the variable ch, char (or character) will be used to store the contents of the file, in this case a character at a time.

The statement while (! OpenFile.eof ()) identifies a cycle, a series of operations to be performed repeatedly until a certain condition occurs, in this case until the file ends:! Indicates to negation while eof stands for "end of file", meaning "end of file". Education can therefore be read as "until the file ends ...."

The operations to be performed until it stops reading are between two braces, they are: OpenFile.get (ch) and court <<ch. Through the function stores the current character you get the file into the variable ch, continue reading, while the court statement allows you to print that character on screen, to verify the proper functioning of the program.

Once completed the cycle, we have to close the stream, or the flow of data from the input file, through education OpenFile.close ().

In the same category ...
E-Learning
ASP (Advanced) ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Web Design (Course) Web Design (Course)
Design Web Sites with HTML, CSS and Dynamic HTML. From 39 €.
Sponsored Links