Discussion:
#include <fstream> using Dev C++// how to specify a file location in windows xp
(too old to reply)
Flip
2004-01-19 20:38:17 UTC
Permalink
im a beginer and i have a simple question. This code is an example
from my text book im trying to understand by changing it, and so far
i've been stuck at this level because i dont understand how it works.

this is a demonstration using <fstream> lib for file IO , and i dont
know the proper syntax for specifying an exact location on the HD that
i would want to save or open a file to.

in this example they are using a file called "infile.dat"..HOW can i
change this to be anything differant. I'm working on XP with Dev C++
..and lets say i wanted to save or open a file from a location in MY
Documents, it would be something like this: C:\Documents and
Settings\endALL\My Documents\File Folder
....ok, so i'll admit, i tried using an addresse with backslashes
like that and was promptly reminded that that is used to tell the
compiler that im giving it an escape sequence..

so how can i tell the compiler (in a way that it will understand)
where i want it to save and open files from?

any information appreciated!

thanks

philip

**heres the code:

#include <stdlib.h>
#include <fstream>

int main()
{
using namespace std;
ifstream in_stream;
ofstream out_stream;

in_stream.open("infile.dat");
if (in_stream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

out_stream.open("outfile.dat");
if (out_stream.fail())
{
:
:

system("PAUSE");

return 0;
}
sirclif
2004-10-20 19:22:52 UTC
Permalink
there are two ways:

1. give out_stream the absolute pathname;

out_stream.open("c:\Documents and Settings\username\My
Documents\outfile.dat");

2. give out_stream the relative pathname;

out_stream.open("..\outfile.dat");
this creates the file in the directory above the directory the
program is excecuting form

im not exactly sure on the windows file nameing, but that is how it would
work in linus
sirclif
2004-10-20 19:23:26 UTC
Permalink
there are two ways:

1. give out_stream the absolute pathname;

out_stream.open("c:\Documents and Settings\username\My
Documents\outfile.dat");

2. give out_stream the relative pathname;

out_stream.open("..\outfile.dat");
this creates the file in the directory above the directory the
program is excecuting form

im not exactly sure on the windows file nameing, but that is how it would
work in linus

Loading...