954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

fstream and saving to a:\

when I complile this program and then try to run it I get error LNK1201. So I started over and left it complile to c:\ and when I try to run it I get file could not be opened. I think the objective is to compile on c:\ and save data file on a:\. what am I doing wrong? thx

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

int main()
{

	char filename[20] = "a:\filename.dat";
	char data[20];

	cout << "Enter the filename: " ;
	cin >> filename;
	cout << endl << endl;

	ofstream outfile;

	outfile.open(filename);

	if (outfile.fail())
	{
		cout << "The file was not successfully opened" << endl;
		exit(1);
	}

	cout << "Input name: ";
	cin >> data;
	cout << endl;

	outfile<<data;
	outfile.close();
	return 0;
}
cap2361
Newbie Poster
13 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

You may need to make it a:\\ because it thinks you are trying to 'escape' a character.

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 
You may need to make it a:\\ because it thinks you are trying to 'escape' a character.

thx but it didn't work. still writes to c:\ drive

cap2361
Newbie Poster
13 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

What exactly are you entering at:

cin >> filename;

If you are not specific (entering drive designator) it will use whatever the default drive is when the application was launched. Probably in your case "c:\".

winbatch is correct with his response, but don't make the mistake of doing the same thing when prompted at the console. Double back slashes have a completely different meaning in that context.

Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 

thanks to both!!!

outfile.open("a:\\filename");
this is line where I needed to have \\ to send to a:\

cap2361
Newbie Poster
13 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You