OPEN A jpg FILE

Hi,

I'm a really low skilled begginer.
I'm tryng to write a code that getting a file name by keyboard open a file (.jpg, .mp3,.avi ...) with that name.

I'm tryng using ShellExecute that works fine for open the file,
but don't know how to write the path of the file with a variable in it...

This is what i've done:

#include <iostream>
#include <windows.h>
#include <shellapi.h>
using namespace std;

int main ()

{
int a,b;
char c[5];
char d[9]= "1234.jpg";

printf ("insetr 4 digit code\n");
scanf("%s", c);
printf("code is %s\n", c);

int i=0;
while (i<4)
{
d[i]=c[i];
i++;
}
c[i]='\0';

printf("open file %s?\n",d);
system("pause");



ShellExecute ( 0,"open","C:\\Users\\Pala\\Desktop\\-filename-",NULL,NULL,SW_NORMAL);

return 0;
}

There is a way to put my variable d as filename?


I hope that what i'm saying is not too stupid!
Sorry for my english as well!

Thanx for any help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstring>
#include <cstdio>

using namespace std;

int main()
{
    char path[] = "C:\\Users\\Pala\\Desktop\\";
    char filename[] = "1234.jpg";

    char buf[256];

    strcpy(buf, path);
    strcat(buf, filename);

    puts(buf);

    return 0;
}

Output:
C:\Users\Pala\Desktop\1234.jpg



See strcpy() and strcat()
http://www.cplusplus.com/reference/cstring/
1
2
std::string file = "C:\\Users\\Pala\\Desktop\\" + d;
ShellExecute ( 0,"open", file, NULL,NULL,SW_NORMAL);
If using std::string, the parameter needs to be
file.c_str()

ShellExecute ( 0,"open", file.c_str(), NULL,NULL,SW_NORMAL);
Thanx very much guys!!!!!

It's working!

I used the code by chervil to create the path with the file name
and then put buf instead of the path in the ShellExecute and it's working!


ShellExecute ( 0,"open", buf, NULL,NULL,SW_NORMAL);

Thanx a lot!!!

Hello again,

I've gone further on with my code...

What I'm tryng to achieve, is a tool that recognise the kind of file by their name's first letter (1=jpg, 2=mp3, 3=avi) and launch the file.
This should work as a loop; when the file is done (the mp3 is finishd...) should be possible to insert a new code and so on...

Problem is:

When the mp3 (or avi or jpg) stops the mp3 player is on top, so if I try to type a new code it wont work...
but it must be like that becouse the final result that i wont is that the user will need only to type and press enter without seeng anything but the file opening :)

How can I do this??

here are my lines:

#include <iostream>
#include <windows.h>
#include <shellapi.h>
#include <cstring>
#include <cstdio>


using namespace std;

int main ()

{
char path[] = "C:\\Users\\Pala\\Desktop\\";
int a,b;
char c[5];
char d[9]= "1234.jpg";
char buf[256];

char tre[2]="3";
char due[2]="2";

char mp3[4]="mp3";
char avi[4]="avi";
int end=0;

while(end<10000000)
{
printf ("insetr code\n");
scanf("%s", c);
printf("code is %s\n", c);


int i=0;
int l=0;

while (i<4)
{
d[i]=c[i];
i++;
}
c[i]='\0';
i=0;

if(d[0]==due[0]){
i=5;
while(i<8)
{
d[i]=mp3[l];
i++;
l++;
}
d[i]='\0';
i=0;
l=0;
}


if(d[0]==tre[0]){
i=5;
while(i<8)
{
d[i]=avi[l];
i++;
l++;
}
d[i]='\0';
i=0;
l=0;
}



printf("open file %s?\n",d);
system("pause");

strcpy(buf, path);
strcat(buf, d);

puts(buf);

ShellExecute ( 0,"open", buf, NULL,NULL,SW_NORMAL);

end++;
}
end=20000000;


return 0;
}



Again I hope that my English is clear enought!

Thanxs
Last edited on
your code is confusing... btw, why you don't using switch?
Topic archived. No new replies allowed.