Trouble with i/o and file opening

I want my program to open a file when you run it, and I've done a lot of searching and found a lot of snippets, but nothing is working. Here's what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <StdAfx.h>
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{

	ofstream picture;
	picture.open ("%userprofile%\nPictures\wedding.jpg");
  return 0;
}



The compiler isn't returning any errors. This isn't the whole program, just the little snippit that's giving me problems.
What do I need to do for this file to open?
What's the actual problem? You say the file doesn't open, but what do you mean?

It is a bit strange that you are opening what you call a JPEG as a text file though.
closed account (DSLq5Di1)
Are you trying to open the file as you would in explorer?
http://msdn.microsoft.com/en-us/library/bb762153
Yes I'm trying to open it in explorer. This seems to be the right syntax for ShellExecute() but it's not working for some reason:
1
2
3
4
5
6
7
ShellExecute(
		NULL,
		_T("open"),                     
		_T("wedding.jpg"),
		_T(" %userprofile%\nPictures\n "),                          
		_T(" C:\ "), 
		SW_SHOWNORMAL);


The error is 'identifier '_T' is unidentified'. Which is fixed if I put 'HINSTANCE' before 'ShellExecute(' but then I get an error for the comma right after '_T("open"),' it says "Expecting a ')'.
Try using TEXT() instead. For some strange reason that has worked for me in the past. I would check your #includes to make sure you have the Windows header that defines the _T() macro.
closed account (DSLq5Di1)
You'll need to escape ("\\") or replace (with forward slashes) those backslashes in your path too, and I'm not sure if ShellExecute will expand environment variables?

-edit-
ShellExecuteEx does though!

1
2
3
4
5
6
7
8
SHELLEXECUTEINFO sei = { sizeof sei };

sei.fMask = SEE_MASK_DOENVSUBST;
sei.lpVerb = TEXT("open");
sei.lpFile = TEXT("%userprofile%\\Pictures\\wedding.jpg");
sei.nShow = SW_SHOWNORMAL;

ShellExecuteEx(&sei);

-edit-
Just noticed also, _T(" %userprofile%\nPictures\n ") you have a space before and after the path name, and new line characters before and after "Pictures"..?
Last edited on
ShellExecuteEx() worked perfectly.
The space before and after the file-path was accidental, I was searching around the forums and found a similar ShellExecute() problem, and in the code they used it had those spaces. I copied and pasted and forgot about it. Whoops!
Thanks for all the help!
Topic archived. No new replies allowed.