I was wondering how to make a program to open a specified file just by entering its location. What command do would i use to open it instead of double clicking on the file. Thanks in advance.

A file can not be opened by merely giving fstream the path. It also need the filename. And use fstream's open() method. Search the net because there are billions of examples.

can you please refer me to a website because all the ones i have been looking at keep showing me different things. If you can give me a website or something to search for i would really appreciate it, thanks.

Well there isn't much to it

int main()
{
   std::string filename;
   cout << "Enter a filename\n";
   getline(cin, filename);
   // now open the file
   ifstream in(filename.c_str());
}

A file can not be opened by merely giving fstream the path. It also need the filename. And use fstream's open() method. Search the net because there are billions of examples.

The filename is part of the "location" of a file :)

I'm confused as to where im supposed to put the the location of the file such as C:\\Documents and Settings\\user\\Desktop\\myfile ??

See the code snippet I gave you. getline() will let you enter it at the keyboard. If you don't want to type it then you can do this: ifstream in("C:\\Documents and Settings\\user\\Desktop\\myfile");

I'm confused as to where im supposed to put the the location of the file such as C:\\Documents and Settings\\user\\Desktop\\myfile ??

you will need a file extension too.

the double \\ are needed for the direct string input but if you

std::string location = "C:\\folder1\\folder2\\my_file.txt";
std::cout << location << std::endl;
//your output will be :
//C:\folder1\folder2\my_file.txt
std::fstream(location.c_str());

you will need a file extension too.

Maybe, and maybe not. Depends on if the file has an extension, and what the actual extension is. Could be *.txt, for might also be something else, or nothing at all. Under MS-Windows XP or newer and *nix it may even have multiple extensions, such as "myfile.abc.efg.txt", in other words filenames may contain more than one period or no periods at all.

Maybe, and maybe not. Depends on if the file has an extension, and what the actual extension is. Could be *.txt, for might also be something else, or nothing at all. Under MS-Windows XP or newer and *nix it may even have multiple extensions, such as "myfile.abc.efg.txt", in other words filenames may contain more than one period or no periods at all.

can you tell me what im doing wrong everytime i compile and run, the command prompt comes up and says press any key to continue and thats it its not open the file

/*
   Opening Files
*/

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() 
{
    std::string location = "C:\\Documents and Settings\\user\\Desktop\\myfile.txt"; 
    std::ifstream in(location.c_str());
   
    system("pause");
    return 0;
}

Edited 6 Years Ago by WaltP: Added CODE Tags

If you run that from command prompt how to you know it isn't opening the file? There is nothing in your program that will tell you whether the file is opened or not. Opening the file will not display its contents on the screen. You have to add more code to do that.

what would i have to write for it to output it onto the screen?

could you help me figure out how to ouput a file onto the screen?

What have you tried? Hint: read a line into a string, output the string (not the only way of course).

Edited 6 Years Ago by jonsca: n/a

This is what i got so far, except idk how to output it onto the screen so i could be able to see it.

/*
   Opening Files
*/
 
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
int main() 
{
    std::string location = "C:\\Documents and Settings\\user\\Desktop\\myfile.txt"; 
    std::ifstream in(location.c_str());
 
    system("pause");
    return 0;
}

Can you read the first line of your file using the getline method? That's what you need to do. Don't over think it, just reread my previous post.

http://www.cplusplus.com/reference/string/getline/

im sorry to tell you but im completely lost??I just cant seem to get this concept, i would appreciate it if you could help me and lead me in the right track

I'm trying to step you through the process, since you do not seem to understand the code that you pasted into post #15.

Try to write a program (it's only going to be one more line than the one that you already have there) that reads in the first line of the file (using the getline method to which I have linked).

I'm trying to step you through the process, since you do not seem to understand the code that you pasted into post #15.

Try to write a program (it's only going to be one more line than the one that you already have there) that reads in the first line of the file (using the getline method to which I have linked).

would this work?

/*
  Opening files
*/

#include <iostream>
#include <fastream>
#include <string>

using namespace std;

int main()
{
    std::string location = "C:/Documents and Settings/user/Desktop/myfile.txt";
    std::ifstream in(location.c_str());
    getline(location,str);
}

Edited 6 Years Ago by rtllz: n/a

Okay. Now can you output that line to the screen?

the first time i compiled and ran it, it did on the command prompt but now it saying that i have an error. The error stating that str is undeclared(first use this function)

the first time i compiled and ran it, it did on the command prompt but now it saying that i have an error. The error stating that str is undeclared(first use this function)

Think about what the error message says, look at your program, and fix it. It the error message says the variable isn't declared, then change the program to declare it. Simple. Any ten-year-old should be able to handle that.

This article has been dead for over six months. Start a new discussion instead.