Hi, this console app asks for a filename, then opens the file with that name (if it exists ofcourse),
then it writes the file content to screen. After that the user may add text to the end of that file...
Now to the question... how do I make the program to behave more lika a text editor which let
you edit the file with more freedom. Like notepad but in a console environment.
Any help is appreciated.
Code:
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
int main()
{
int radnr=1;
char filename[256], yn='y', thetext[500];
while (yn=='y')
{
cout << "Filename: ";
cin >> filename;
ifstream text;
text.open(filename);
if (!text)
{
cerr << "Could not open file!" << endl;
}
else
{
for (;!text.eof(); radnr++)
{
text.getline(thetext, 500);
cout << setw(4) << setfill('0') << radnr << " "
<< thetext << endl;
}
text.close();
ofstream skriv(filename, ios::app);
for (;true; skriv << thetext << endl)
{
cin.getline(thetext, 500);
if (strcmp (thetext, "/break")==0)
return 0;
}
skriv.close();
}
cout << "Open another file? (y/n)" << endl;
yn=getch();
}
return 0;
}