Very simple question, but neither my book nor this site is very clear on how to do something that simple.
I have a .txt, let's call it "textfile.txt". Let's also assume that the text "Hello, World!" is written in that file. Finally, assume that I'm running a console program.
How would I make the program display the contents of the file? How would I make the program open the file? (If the default program to open textfiles was NotePad then the program would open textfile.txt on NotePad.)
The file should be in the same directory as your executable. You can have it a level up or two but you must show that in the string.
1 2 3
ifstream myfile ("textfile.txt"); // Looks in the directory that is the same as the EXE
myfile.open("../textfile.txt"); // Looks up one level of the current directory (into its Parent)
myfile.open("txt/textfile.txt"); // Looks in the sub-directory "txt" for the file "textfile.txt"
What happens to a text file when you double-click it on a standard Windows machine is that some other programme gets run (for example, notepad) and then that programme will open the file and do whatever it does by default with them - in this case, display it to you in a text editor.
Yes, what I meant was basically making the program till my computer to look up the .txt file and open it with whatever program is used to normally open it if I had double-clicked on the file. In other words, make my program tell the computer to open textfile.txt in a text editor like NotePad.
That is something controlled by your operating system (or more precisely, whatever you use as your graphical file navigator, which in Windows is typically explorer.exe, I think); the file itself can carry information identifying what kind of file it is, but your operating system (or file navigator) decides what to do with it in response to a double-click. If you want your operating system (or file navigator) to get that information from the file itself, you'll have to change how your operating system (or file navigator) works.
If you want some programme of your own to do that, you will have to tell your operating system (or file navigator) to open the file with your programme, and then have your programme start the programme you actually want (e.g. some text editor) and feed it the file.
I understand, thank you :) Although that might be beyond me and is at the moment irrelevant, so I'd sya my questions have been answered. Thanks very much! :)