Read files

Hello all,

I use Visual Studio 2012 for compiling C++. I created a win.32 console app and made a text file named "Mydata.txt" and wrote into it and then closed the file and then the project. Now I want to read that "Mydata.txt" file from another new project. How can I tell the new project/program that where to access and then read that "Mydata.exe" file?

Thanks.
You just have to give it the path to the file.

You can give it the absolute path:
 
ifstream file("C:/path/to/the/file/Mydata.txt");


or you can give it a path relative to the current directory:
 
ifstream file("../../the/file/Mydata.txt");



EDIT:
Or you can change the current directory to be the same as it was for the other program.
Last edited on
Thank you very much.
This is my simple code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "C:\Users\Myname\Desktop\Documents\std_lib_facilities.h"
using namespace std;

//**************************************************

int main()
	try {

ifstream ifs("C:\Users\Myname\Desktop\raw_temps.txt");
if(!ifs) error("can't open file raw_temps.txt");
keep_window_open("~~");
  return 0;
}

//**************************************

catch(runtime_error& e) {
	cerr<<e.what();
	keep_window_open("~~");
	return 1;
}

The .txt file is in address "C:\Users\Myname\Desktop\raw_temps.txt".
When I run that, only the error (" ... ") function operates and theifs can't open the raw_temps.txt file.
Why?
Use / forward slashes... not \ back slashes.

The backslash is the escape character, so it thinks this:
Desktop\raw_temps.txt"


is this:
Desktop<carriage return>aw_temps.txt"



If you must you backslashes, you have to double them:


...MyName\\Desktop\\raw_temps.txt


But again.. it's easier to just use forward slashes.
Last edited on
Thanks too much. Yes I like to use forward slash.
Topic archived. No new replies allowed.