How to read a file located at C: drive?

Hello, my name is Morgan and I am starting to learn visual studio c++ and would
like to request help with an issue I am having. I have read a few threads on this
forum and they are really helpful so I feel like you guys will give me an answer
at your earliest convenience.

I am dearly sorry if this has been asked/posted before.

Here's my code which is things I am learning right now as a beginner..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char ch;

	ifstream infile("C:\\test.txt");
	ofstream outfile("C:\\test1.txt");

	if (!infile)
		clog << "File not found" << endl << endl; 
	else
		clog << "File succesfully found..." << endl << endl;
	if (!outfile)
		clog << "New file could not be written" << endl << endl;
	else
		clog << "New file succesfully written." << endl << endl;

	while (infile && infile.get(ch) )
		outfile.put(ch);


The if statement works fine and so does the last part for writing the file.
However, I've tested it out without adding the "C:\\" to the file names
and it worked fine, but when I try and set the location to read the input
file, it doesn't work, it ends up writing "test1.txt" as a blank file, presumably
because it could not read the input file.

So my question to you is, how do I set the location in which the program will
try and find the file with the given name?
Last edited on
The standard library doesn't give you a way, as far as I know. You'd have to use either an additional library or get into some platform specific code.

On Windows, you can set this directory with the SetCurrentDirectoryA function:

1
2
3
4
5
6
7
#include <windows.h>

// ...

SetCurrentDirectoryA( "C:\\Look\\In\\This\\Directory\\" );

ifstream infile( "test.txt" ); // looks in above specified directory 


(note: the 'A' in SetCurrentDirectoryA is not a typo. That specifies you want the ASCII version. Leaving off the A gives you the TCHAR version. It's complicated -- just leave the A on there)
@Disch

This code works for me on windows . With out setting any directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std; 



int _tmain(int argc, _TCHAR* argv[])
{
	char ch;
	ifstream ifin("E:\\testin.txt");
	ofstream ofin("G:\\testout.txt");
	if(!ifin)
		cout<< "\n input file not opened successfully ";
	if(!ofin)
		cout<<"\n output file not opened successfully";

	while(ifin && ifin.get(ch) )
		ofin.put(ch);

	return 0;
}
Last edited on
Well yeah but you're specifying the full directory when you open the file. I thought the point here was he didn't want to do that?
I don't mind specifying the full path, it's just that whenever I tried doing the full path it never
worked. Possibly an error with the way I typed it. I'll try bluecoders thing once I get back home.
I tried yours Disch and it worked for input file, but not output.
@MorganAW - I assume you're using a new-ish version of Windows. SInce Vista, access to the system root is restricted (the roots of other drives are fair game, though). You can get round it if you're keen enough, by granting yourself the right privileges, but you're not supposed to write files there.

By the way, you did ensure that the target directory already existed when trying the output case? You see, the ostream open commands can't deal with directory creation. And SetCurrentDirectory only works when the directory is there, too.

Andy

PS Isn't chdir() the standard (or as least Posix) equivalent to SetCurrentDirectory()? (And it's prob become _chdir() with newer versions of VC++)
Last edited on
Topic archived. No new replies allowed.