-
Simple word processor.
Im a novice in the C++ programming language and ive been using the book C++ Without Fear to help myself get a grip on the language. One of the activities in the book is writing a Word processor "like" program.
What I'd like to do is write it so that the user doesn't have to enter a preexisting file name to begin. I'd like to have the program create a file with the name the user inputs and save it to a Folder that exists already.
Also I'm sure this is much more complicated to do but later on I'd like to be able to add a feature where it will print what you input when your done. Any tips on how to accomplish this would be appreciated...sorry i didnt think about defining print but yes i meant onto physical paper. As it turns out your right it will create the file i just didnt know it. o.o
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char filename[81];
char input_line[81]; // Input line for text entry
cout << "Enter a file name and press ENTER: ";
cin.getline(filename, 80);
ofstream file_out(filename);
if (! file_out) {
cout << "File " << filename << " could not be opened."; //if file opening fails
return -1;
}
cout << "File " << filename << " was opened." << endl;
while (1) {
cout << "Enter line (~~~ to quit)>>";
cin.getline(input_line, 80);
if (strcmp(input_line, "~~~") == 0)
break;
file_out << input_line << endl;
}
file_out.close();
return 0;
}
-
From a quick look at your code, it seems that it should already create a new file if it doesn't exist already.
When you say print, do you mean onto the screen, or onto physical paper? Assuming you meant the paper, this (to my knowledge) is platform specific. If you're on windows, I know the win32 API has functions that does this. Check MSDN if you're really keen, though it may not be as simple as you think.
-
Perhaps consider an edit class with member functions like
- load file
- save file
- insert line
- delete line
- ditto for characters.
- other methods you can think of.
This implements the model
-
@Salem Yea i was thinking about doing some stuff like that but i think its probably beyond my capabilities for now at least. Also im new here and was wondering is there a double posting rule?