Reading from a file and opening a file

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.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("textfile.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


This will read the file, line by line, and output the contents to console. It's from this page:

http://www.cplusplus.com/doc/tutorial/files/
Read it, did it, doesn't work. Does the file need to be in a specific location or can it be almost anywhere on the computer?
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" 
Last edited on
Ah, so that's the problem =) Suspected it'd be something like that but wasn't sure exactly where to place it.

So how do I open the file itself rather than print its contents on the screen? Or is that an entirely different thing?
What does to "open a file" mean to you?

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.
Last edited on
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.
Last edited on
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! :)
Topic archived. No new replies allowed.