File Openings

Pages: 123
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.

Any help at all will be much appreciated!
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?
Dude it's all there in that link :)
Here's what you can do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>

using namespace 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?
From what I know, that's not possible.

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:

ofstream out("output.txt", ios::app);
Last edited on
I want the text file to literally open up when the user inputs Open
If you are using Windows:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb776886%28v=vs.85%29.aspx
I looked at that link and I don't really understand it. Like what header files are involved and what is the syntax?
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);
Ok, now it makes sense! The thing that confused me was the ShellExecute. I just wasn't sure how to implement that! xD
And so i dont need any new specific header files in order to accomplish the ShellExecute function?
MiiNiPaa wrote:
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 my file is in my C drive, then rather than putting a D: I should put a C:?
Yes, you put complete path to your file here.
Oh, so not just C:\Password_File.txt?
So I have to type: C:\Users\battl_000\Desktop\PASSWORD_FILE.txt
?
Yes, you have to type it exactly like you would type it in, say, cmd.exe to open it.
Pages: 123