Hey guys, so I need to create a program that will prompt the user for an input, then it will save their input to a file and then it will prompt the user to input "Open" and then the file containing their first input, will open up and show their input.
I read it and I think I need the one with the: Reading from a file. But can someone please explain it to me so I understand it? like what each line does?
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(){
string input;
cout << "Enter the input: ";
getline(cin, input, '\n');
cout << endl;
// Create a new file named "output" and write the input to it
ofstream out("output.txt");
out << input;
out.close();
cout << "Enter 'Open' to display your input from file: ";
getline(cin, input, '\n');
cout << endl;
if (input == "Open" || input == "open"){
string inputRead;
// Open "output.txt" file and read the first line
ifstream in("output.txt");
getline(in, inputRead, '\n');
cout << endl << inputRead << endl;
}
system("PAUSE");
}
So wait, do I need to create an output.txt BEFORE i run this program, OR when I run this program, will it create a text file called output.txt and write in that immediately?
Ok i ran ur program. it works great just one thing. i dont want it to display whats inside the txt file in the cmd. I want the text file to literally open up when the user inputs Open, with their password in it. How do i do tht?
To answer your previous question, the file will be created automatically. If it already exists, its content will be overwritten. If you don't want to overwrite the contents, use this:
what header files are involved and what is the syntax?
http://puu.sh/epLfJ/a07b709b93.png
Hm. It looks like I supposed to use ShellExecute to do the task... Hey, it is a link! http://puu.sh/epLjX/d19fb47611.png
And it have syntax description. And extensive description of different parameters. And required headers (and for almost all windows API you can just include windows.h).
And if for some reason you still not understand, this will open text file "D:/sample.txt" in default text editor: ShellExecute(NULL, "open", "D:/sample.txt", NULL, NULL, 0);
Hey, it is a link! http://puu.sh/epLjX/d19fb47611.png
And it have syntax description. And extensive description of different parameters. And required headers (and for almost all windows API you can just include windows.h).