In: Computer Science
DATA STRUCTURE C++ LANGUGAE
Create a notepad that allows the user to write text (char by
char) on the console. For this
purpose, the user should be able to control and track the movement
of the cursor. The user
should be able to navigate to any part of console (cursor control)
in order to perform any
insertion or deletion of words.
Internally, the notepad is composed of two-dimensional linked list
(every node should
comprise of data element (char) and 4 pointers for navigating left,
right, up, and down). Since
text can be written on multiple lines, each row of the 2D linked
list represents one line. Each
line is terminated when a newline is inserted in the ADT.
First of all we need to know the basic points of creating file in C++
The fstream
library allows us to work with
files.
To use the fstream
library, include both the
standard <iostream>
AND the
<fstream>
header file:
Object/Data Type | Description |
---|---|
ofstream |
Creates and writes to files |
ifstream |
Reads from files |
fstream |
A combination of ofstream and ifstream: creates, reads, and writes to files |
To create a file, use either the ofstream
or
fstream
object, and specify the name of the file.
To write to the file, use the insertion operator
(<<
).
#include <iostream>
#include <fstream>
using namespace std;
Now To READ a file:
To read from a file, use either the ifstream
or
fstream
object, and the name of the file.
Note that we also use a while
loop together with
the getline()
function (which belongs to the
ifstream
object) to read the file line by line, and to
print the content of the file:
// Create a text string, which is used to output the text
file
string myText;
// Read from the text file
ifstream MyReadFile("filename.txt");
// Use a while loop together with the getline() function to read
the file line by line
while (getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText;
}
// Close the file
MyReadFile.close();